Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copy Constructor CSCE 121 J. Michael Moore.

Similar presentations


Presentation on theme: "Copy Constructor CSCE 121 J. Michael Moore."— Presentation transcript:

1 Copy Constructor CSCE 121 J. Michael Moore

2 Overload Constructor Constructor that takes an object of the same type as a parameter. Ensure deep copy rather than default shallow copy.

3 Using Copy Constructor
MyClass object1; MyClass object2 = object1; MyClass object3(object1);

4 Declaration MyClass(const MyClass& source);

5 Definition MyClass::MyClass(const MyClass& source) { // Allocate new memory // Copy data from source to new memory }

6 Calls copy constructor,
Don’t be deceived MyClass object1; MyClass object2 = object1; Calls copy constructor, Not copy assignment!


Download ppt "Copy Constructor CSCE 121 J. Michael Moore."

Similar presentations


Ads by Google