Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3330 Object-oriented Programming in C++

Similar presentations


Presentation on theme: "COP 3330 Object-oriented Programming in C++"— Presentation transcript:

1 COP 3330 Object-oriented Programming in C++
Classes and Objects: Automatics, copy constructor, assignment Spring 2019

2 Automatic Functions In C++, some default functions are automatically built if you do not provide them Constructor Destructor Copy Constructor (move constructor) Assignment operator= (move assignment) The automatic versions of the constructor and destructor don’t do anything, they are created as blank functions

3 Copy Constructor Is a constructor
a function with the same name as the class and no return type (just like any other constructors) Invoked implicitly when a COPY of an existing object is created, when: An object is defined to have the value of another object of the same type An object is passed by value into function An object is returned by value from a function Example Fraction f1, f2(3,4); Fraction f3 = f2; // copy constructor called to declare and initialize f3 Remember that anything that is passed by value in and out of functions causes copies to be made!)

4 Declaration and Definition
A copy constructor creates a new object, initialized as a copy of another existing object Original object passed in as parameter (copy constructor always has one parameter of the same type) Passed by reference, since passing by value will invoke the copy constructor and this is what we are trying to define! Format className(const className&); The const is not required, but a good idea for protecting the original Examples: Fraction(const Fraction& f); Timer(const Timer& t); Directory(const Directory& d);

5 Shallow Copy vs. Deep Copy
The default version of the copy constructor (created by the compiler) makes a shallow copy Each member data value is copied exactly as it is over to the corresponding member data location in new object Sufficient for classes like Fraction Only has a private data int numerator and int denominator NOT sufficient for pointer member data Example: Phonebook The entryList pointer will be copied and will point to the original dynamically allocated memory

6 Visualize the Execution
int main() { … stack (high address) main heap (low address)

7 Visualize the Execution
int main() { Directory d; …… stack (high address) main int maxSize = 5 int curentSize = 0 Entry *entryList = d Entry[5] heap (low address)

8 Shallow Copy int main() { Directory d; Directory d2 = d; ……
stack (high address) main int maxSize = 5 int curentSize = 0 Entry *entryList = d int maxSize = 5 int curentSize = 0 Entry *entryList = d2 Entry[5] heap (low address)

9 Problems of Shallow Copy
Potential inconsistencies Non dynamically allocated variables are copied verbatim Dynamically allocated variables point to the same memory region -> changes in one object will affect the other Free memory twice, when going out of scope Runtime error !

10 Deep Copy Creates a copy of dynamically allocated memory
Directory::Directory(const Directory &d) { maxsize = d.maxsize; currentsize = d.currentsize; entryList = new Entry[d.maxsize]; for (int j = 0; j < currentsize; j++) entryList[j] = d.entryList[j]; }

11 Deep Copy int main() { Directory d; Directory d2 = d; ……
stack (high address) main int maxSize = 5 int curentSize = 0 Entry *entryList = d int maxSize = 5 int curentSize = 0 Entry *entryList = d2 Entry[5] Entry[5] heap (low address)

12 Assignment Operator (=)
operator= is similar to the copy constructor Called when one existing object is assigned to another existing object Fraction f1, f2; f1 = f2; Assignment operator also has to make a copy The default version makes a shallow copy A deep copy needs to be overloaded by the user Like a copy constructor, operator= will take one parameter of the same object type Directory& operator=(const Directory&); Fraction& operator=(const Fraction&);

13 Copy Constructor vs. Assignment Operator
Initializes a new object as a copy of an existing one Initializes data for the first time Has no return value Sets the current state of an existing object to that of another existing object May need to free up old dynamically allocated memory Returns the value that was assigned to support a = b = c = 4; Must be a member function

14 Chained Assignment a = b = c = 4; Same as a = (b = (c = 4));
Thus, (c = 4) returns c by reference, therefore returns 4

15 The this Pointer Inside a member function
An object can access its own address using the this pointer this is a keyword in C++ To return the object itself by reference, return the target of the this pointer, or *this

16 Directory operator= Implementation
Directory& Directory::operator=(const Directory &d) { if (this == &d) return *this; // don’t self copy // if you don’t check this, this can self destruct… delete [] entryList; maxsize = d.maxsize; currentsize = d.currentsize; entryList = new Entry[d.maxsize]; for (int j = 0; j < currentsize; j++) entryList[j] = d.entryList[j]; return *this; }

17 Fraction Example No need for copy constructor and operator=
Fraction does NOT have dynamically allocated memory Automatic versions are sufficient with the use of shallow copies Fraction::Fraction(const Fraction &f) { numerator = f.numerator; denominator = f.denominator; } Fraction &Fraction::operator=(const Fraction &f) return *this;

18 Questions


Download ppt "COP 3330 Object-oriented Programming in C++"

Similar presentations


Ads by Google