cs32w09dis1 - 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 3A Notes (Week 2, January 16) 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 the 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 CS32: Introduction to Computer Science II Winter 2009 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 DocumentRight Arrow Icon
almost sounds right, but there is a pitfall here -- it even copies the value of a pointer, resulting in a 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 her name (I know, I know. This surely is a bad interface, but I just made it that way). 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 (this is not really a CS term, btw) of an object is something that occurs over and over, so we would like to do it in an efficient way. A copy
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.

This note was uploaded on 11/07/2009 for the course CS 32 taught by Professor Davidsmallberg during the Winter '08 term at UCLA.

Page1 / 6

cs32w09dis1 - CS32 Introduction to Computer Science II...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online