{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Session_12 - Not from Textbook The Copy Constructor In...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Not from Textbook The Copy Constructor In addition to providing a default constructor and destructor, the compiler provides a default copy constructor. The copy constructor is called every time a copy of an object is made. When you pass an object by value, either into a function or as a function’s return value, a temporary copy of that object is made. If the object is a user-defined object, the class’s copy constructor is called . All copy constructors take one parameter, a reference to an object of the same class. It is a good idea to make it a constant reference because the constructor will not have to alter the object passed in. For example, CAT(const CAT & theCat); In the above example, the CAT constructor takes a constant reference to an existing CAT object. The goal of the copy constructor is to make a copy of theCat. The default copy constructor simply copies each member variable from the object passed as a parameter to the member variables of the new object . This is called a member-wise (or shallow) copy, and although this is fine for most member variables, it breaks pretty quickly for member variables that are pointer to objects on the free store. Let’s begin without using the free store: #include <iostream> using namespace std; class CAT { public : CAT(); // default constructor ~CAT(); // default destructor int GetAge() const { return itsAge; } void SetAge ( int age) {itsAge = age; } private : int itsAge; }; CAT::CAT() // default constructor { itsAge = 5; } CAT::~CAT() // default destructor { } int main() { CAT frisky; cout << "frisky's age: " << frisky.GetAge() << endl; Page 1 of 15
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
cout << "Setting frisky to 6...\n" ; frisky.SetAge(6); cout << "Creating boots from frisky\n" ; CAT boots(frisky); // uses the default copy constructor cout << "frisky's age: " << frisky.GetAge() << endl; cout << "boot's age: " << boots.GetAge() << endl; cout << "setting frisky to 7...\n" ; frisky.SetAge(7); cout << "frisky's age: " << frisky.GetAge() << endl; cout << "boot's age: " << boots.GetAge() << endl; return 0; } Everything is fine in this case. Let’s try to do the same using the free store: #include <iostream> using namespace std; class CAT { public : CAT(); // default constructor ~CAT(); // default destructor int GetAge() const { return *itsAge; } void SetAge ( int age) {*itsAge = age; } private : int *itsAge; }; CAT::CAT() // default constructor { itsAge = new int ; *itsAge = 5; } CAT::~CAT() // default destructor { } int main() { CAT frisky; cout << "frisky's age: " << frisky.GetAge() << endl; cout << "Setting frisky to 6...\n" ; frisky.SetAge(6); cout << "Creating boots from frisky\n" ; CAT boots(frisky); // uses the default copy constructor cout << "frisky's age: " << frisky.GetAge() << endl; cout << "boot's age: " << boots.GetAge() << endl; cout << "setting frisky to 7...\n" ; frisky.SetAge(7); cout << "frisky's age: " << frisky.GetAge() << endl; cout << "boot's age: " << boots.GetAge() << endl; return 0; } In this case, we have a problem, as we are using a “shallow copy”. A shallow or member- wise copy copies the exact values of one object’s member variables into another object.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern