cs32w11dis1 - CS32 Introduction to Computer Science II...

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

View Full Document Right Arrow Icon
Discussion 2C Notes (Week 2, January 14) TA: Brian Choi ([email protected] ) Section Webpage: Copy Constructors We know what constructors do. They are called when an object is created, and are used to initialize the members of the object -- they set up the object so that it is ready to be used. Now, take a look at the following class definition: class School { public: School(const string &name); string getName() const; void setName(const string &name); void addStudent(const Student &student); Student *getStudent(const string &name) const; bool removeStudent(const string &name); int getNumStudents() const; private: string m_name; // Name of the school. Student *m_students; // Dynamic array of students. int m_numStudents; // Number of students. }; Assuming there is a class called Student , this class maintains a dynamically allocated array of students, and can return a pointer to the student object given the student’s name. For instance, I can do the following: Student st1("Brian"); Student st2("John"); School s1("UCLA"); s1.addStudent(st1); s1.addStudent(st2); Student *p = s1.getStudent("John"); Our mission: I want to create another object of type School called s2 with the exactly same values as s1 , called. Given the above definition, what would you do? Candidate 1 School s2(""); s2 = s1; This sounds like an obvious option. But let us think about the default behavior of the assignment operator ( = ). When applied to a class, it copies the value of every member variable to the new object ( s2 ). This almost sounds right, but there is a pitfall here -- it even copies the value of a pointer, resulting in the CS32: Introduction to Computer Science II Winter 2011 Copyright 2011 Brian Choi Week 2, Page 1/6 !" $%&'
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
following (undesired) result. Therefore, we cannot rely on this default behavior and need another way of doing this. Candidate 2 You can try to use all the accessor functions available to you to set the values of s2 . Can this be done? The only way to get a Student object out of a School object is to specify the name associated to it (yes, I admit this is a bad interface). Often, you simply don’t have accessors and modifiers to all the member variables. Even when you do, it is considered a poor design to require the user to know all the implementation details. It is also cumbersome (especially if there are many variables to copy) and does not let you employ a different implementation without changing the public interface, thus losing the benefit of have the abstraction. Computers are meant to facilitate work, and computer scientists are lazy. They’d rather do a hard work once and forget about doing the same for the rest of their lives. Cloning an object is a task that you get to do over and over, so we would like to do it in an efficient way. A copy constructor is designed to do this job for us.
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