Unformatted text preview:

Object Oriented Programming Using C CLASS 13 Honors 1 Objectives Lists reasons why you would use a copy constructor and an overloaded assignment operator Use the this pointer Lists rules for overloading operators 2 Copy Constructors A copy constructor is a special constructor called whenever a new object is created and initialized with another object s data PersonInfo Person1 Maria JonesTucker 25 PersonInfo 3 4 5 Using const Parameters Because copy constructors are required to use reference parameters they have access to their argument s data They should not be allowed to change the parameters therefore it is a good idea to make the parameter const so it can t be modified PersonInfo const PersonInfo Obj Name new char strlen Obj Name 1 strcpy Name Obj Name Age Obj Age 6 The Default Copy Constructor If a class doesn t have a copy constructor C automatically creates a default copy constructor The default copy constructor performs a memberwise assignment 7 Operator Overloading C allows you to redefine how standard operators work when used with class objects 8 Overloading the Operator void operator const PersonInfo Right Will be called with a statement like Person2 Person1 Or Person2 operator Person1 Note that the parameter Right was declared as a reference for efficiency purposes the reference prevents the compiler form making a copy of the object being passed into the function Also note that the parameter was declared constant so the function will not accidentally change the contents of the argument 9 This program demonstrates the overloaded operator include iostream include cstring For strlen using namspace std class PersonInfo private char name int age public PersonInfo char n int a name new char strlen n 1 strcpy name n age a 10 Program continues PersonInfo const PersonInfo obj Copy constructor name new char strlen obj name 1 strcpy name obj name age obj age PersonInfo void delete name char getName void return name int getAge void return age 11 Program continues void operator const PersonInfo right delete Name name new char strlen right name 1 strcpy name right name age right age 12 Program continues int main PersonInfo Jim Jim Young 27 Bob Bob Faraday 32 Clone Jim cout The Jim Object contains Jim getName cout Jim getAge endl cout The Bob Object contains Bob getName cout Bob getAge endl cout The Clone Object contains Clone getName cout Clone getAge endl cout Now the clone will change to Bob and cout Bob will change to Jim n Clone Bob Call overloaded operator Bob Jim Call overloaded operator 13 Program continues cout The Jim Object contains Jim getName cout Jim getAge endl cout The Bob Object contains Bob getName cout Bob getAge endl cout The Clone Object contains Clone getName cout Clone getAge endl return 0 14 Program Output The Jim Object contains Jim Young 27 The Bob Object contains Bob Faraday 32 The Clone Object contains Jim Young 27 Now the clone will change to Bob and Bob will change to Jim The Jim Object contains Jim Young 27 The Bob Object contains Jim Young 27 The Clone Object contains Bob Faraday 32 15 The Operator s Return Value If the operator function returns a void as in the above example then multiple assignment statements won t work To enable a statement such as Person3 Person2 Person1 You must have the following prototype PersonInfo operator const PersonInfo Right 16 The this Pointer this is a special built in pointer that is available in any member function this contains the address of the object that called the member function The this pointer is passed as a hidden argument to all non static member functions 17 This program demonstrates the overloaded operator include iostream include cstring For strlen using namespace std class PersonInfo private char name int age public PersonInfo char n int a name new char strlen n 1 strcpy name n age a 18 Program continues PersonInfo const PersonInfo obj Copy constructor name new char strlen obj name 1 strcpy name obj name age obj age PersonInfo void delete name char getName void return name int getAge void return age 19 Program continues PersonInfo operator const PersonInfo right delete name name new char strlen right name 1 strcpy name right name age right age return this 20 Program continues int main PersonInfo Jim Jim Young 27 Bob Bob Faraday 32 Clone Jim cout The Jim Object contains Jim getName cout Jim getAge endl cout The Bob Object contains Bob getName cout Bob getAge endl cout The Clone Object contains Clone getName cout Clone getAge endl 21 Program continues cout Now the clone and Bob will change to Jim n Clone Bob Jim Call overloaded operator cout The Jim Object contains Jim getName cout Jim getAge endl cout The Bob Object contains Bob getName cout Bob getAge endl cout The Clone Object contains Clone getName cout Clone getAge endl return 0 22 Program Output The Jim Object contains Jim Young 27 The Bob Object contains Bob Faraday 32 The Clone Object contains Jim Young 27 Now the clone and Bob will change to Jim The Jim Object contains Jim Young 27 The Bob Object contains Jim Young 27 The Clone Object contains Jim Young 2 23 Some General Issues of Operator Overloading You can change an operator s entire meaning when you overload it But don t You cannot change the number of operands taken by an operator For example the symbol must always be a binary operator Likewise and must always be unary operators You cannot overload the and sizeof operators 24 Contents of FeetInches H include stdlib Needed for abs ifndef FEETINCHES H define FEETINCHES H using namespace std class FeetInches private int feet int inches void simplify 25 public FeetInches int f 0 int i 0 feet f inches i Simplify void setFeet int f feet f void setInches int i inches i Simplify int GetFeet void return feet int GetInches void return inches 26 FeetInches operator const FeetInches Overloaded FeetInches operator const FeetInches Overloaded endif 27 Contents of FeetInches CPP include FeetInches h void FeetInches simplify void if inches 12 feet inches 12 Integer division inches inches 12 else if inches 0 feet abs inches 12 1 inches 12 abs inches 12 28 Program continues Overloaded binary operator FeetInches FeetInches operator const FeetInches right FeetInches temp temp inches inches right inches temp feet feet right feet temp simplify return temp 29 Program continues Overloaded binary operator FeetInches FeetInches operator const FeetInches right FeetInches temp temp inches inches right inches temp feet feet right feet temp simplify return temp 30


View Full Document

SMU CSE 2341 - Object-Oriented Programming Using C++

Loading Unlocking...
Login

Join to view Object-Oriented Programming Using C++ and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Object-Oriented Programming Using C++ and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?