210 likes | 333 Views
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
E N D
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 • Overloading the “=“ operator • Handling strings.
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.
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.
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.
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);
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;}
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;}
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
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
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.
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();
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.
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; } };
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.
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
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; }
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 !
Service objects • Possible to have an object that provides a service. • e.g. Tracks and provides account numbers. • See “BankManager.cpp” & “AccNum.h”
Summary • This week have looked at • Copy Constructors • Default. • User Defined. • Operator Overloading • Overloading the << operator. • Overloading the + operator. • Service Objects