1 / 20

C++ Programming for Graphics

C++ Programming for Graphics. Lecture 6 Copy Constructors & Overloading Operators. Introduction. This week…. Copy Constructors How to copy one object to another. Default copy constructor. User Defined copy constructor. Overloading Operators Overloading the “<<“ output operator

lucio
Download Presentation

C++ Programming for Graphics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ Programming for Graphics Lecture 6 Copy Constructors & Overloading Operators

  2. Introduction • This week…. • Copy Constructors • How to copy one object to another. • Default copy constructor. • User Defined copy constructor. • Overloading Operators • Overloading the “<<“ output operator • Overloading the “=“ operator • Handling strings.

  3. Copy Constructors • Copy constructors allow one object to be copied from a second. • System provides a default copy method. • Copies attributes • e.g. Account Freddy(12.36, 1.5, 1); Account Tripta; Tripta = Freddy; • Of questionable use in this example – same account numbers.

  4. Default Copy Constructor • Warning • When using a default Copy Constructor any existing values will be overwritten. • e.g. Account Freddy(12.36, 1.5, 1); Account Tripta(15.27, 1.6, 2); Tripta = Freddy; • Tripta becomes a “clone” of Freddy.

  5. Copy Constructors • Just as able to define constructors. • Possible to define copy constructors. • Takes name of class • Accepts a single argument – reference to same class type. • Can accept other arguments. • Within implementation – access source object’s members via the reference.

  6. Example (heavily abridged) • Within the classclass Clock{public: Clock(Clock& clk) { nHour = clk.nHour + 1; nMins = clk.nMins + 1; nSecs = clk.nSecs + 1; }};call – Clock myClock2(myClock1);

  7. Example - cont • Hiding the implementationclass Clock{public: Clock(Clock&);};// Implementationinline Clock::Clock(Clock& clk){ nHour = clk.nHour + 1; nMins = clk.nMins + 1; nSecs = clk.nSecs + 1;}

  8. Example – cont (additional args) • Hiding the implementationclass Clock{public: Clock(Clock&, int);};// Implementationinline Clock::Clock(Clock& clk, int nVal){ nHour = clk.nHour + 1; nMins = clk.nMins + 1; nSecs = nVal;}

  9. Operator Overloading • Have already examined how functions, methods & constructors can be overloaded. • Also possible to overload most operators • e.g. “+” “-” “=“ “<<“ • You should check your text books to determine which operators can not be overloaded

  10. Operator Overloading • Many operators are already overloaded. • e.g. “<<“ • In ‘C’ - Bitwise shift to the left. • In ‘C++’ - As in ‘C’ plus…. • - Directing output to ostream. • As with function overloading – system determines correct usage from arguments

  11. Operator Overloading • Within ‘C++’ “<<“ has been overloaded a number of times • It can handle many different data types. • “Common” operators are also already overloaded • e.g. “+” • can handle int, float, double etc.

  12. User defined operator overloading • Why? • Have already seen how to show details of an object • e.g. cout << Freddy.getBalance() << Freddy.getIntRate() << endl; • Could also have an output method. • e.g. Freddy.printDetails();

  13. Overloading operators • So far, so good • But, can simplify by using operator overloading. • E.g. • cout << Freddy << Tripta << Hassan << endl; • How? • By overloading the “<<“ operator • Supply an “object output” definition.

  14. Overloading << for Clock class Clock{ int nHours; int nMins; int nSecs; public: friend ostream& operator<<(ostream& os, Clock& clk) { os << clk.nHour << “:” << clk.nMins << “:” << clk.nSecs << endl; return os; } };

  15. Limitations • When overloading operators you can not • Alter its precedence. • Can use parentheses. • Alter its associativity. • e.g. can not change a “left to right” operator to “right to left” • Alter its “arity” • The number of operands an operator takes.

  16. Overloading = • Used instead of the default copy. • e.g. Object1 = Object2 • Instead of memberwise assignment. • Possible to define how the assignment operator functions for a given operator

  17. This amounts to a copy – could easily change this. Overloading = for Clock class Clock { int nHours; int nMins; int nSecs; public: const Clock& operator=(const Clock& clk) { if(&clk != this) // check for self assignment { nHour = clk.nHour; nMins = clk.nMins; nSecs = clk.nSecs; } return *this; // to allow – for example // myClock1 = myClock2 = myClock3; }

  18. Strings • Handling Strings in C is not easy. • Have to use pointer / array notation. • Much easier if string type. • As with int float double etc…. • C++ does not offer a string type • But…… • Offers the facility to build your own • Covering this in next lecture • Get on with operator overloading !

  19. Service objects • Possible to have an object that provides a service. • e.g. Tracks and provides account numbers. • See “BankManager.cpp” & “AccNum.h”

  20. Summary • This week have looked at • Copy Constructors • Default. • User Defined. • Operator Overloading • Overloading the << operator. • Overloading the + operator. • Service Objects

More Related