{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


cs32s10dis1 - CS32 Introduction to Computer Science II...

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

View Full Document Right Arrow Icon
Discussion 1B Notes (Week 2, April 9) TA: Brian Choi ([email protected] ) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 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(); 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 numStudents; // number of students }; Assume 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”); 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 Student 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 variables 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 Spring 2010 Copyright 2010 Brian Choi Week 2, Page 1/6 !" $%&’
Background image of page 1

Info iconThis 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? For School , the only way to get a Student object out of it is to specify the name associated to it (Yes, this surely is a bad interface, but I designed it that way on purpose to demonstrate this problem). Often you simply don’t have accessors and modifiers to all the member variables. Even if you do, it is a poor design to force the user to learn all the implementation details. Lastly, it is cumbersome (especially if there are many variables to copy) and slows down the program a little (why?). 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 that job for us.
Background 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 ]}