{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

dis1 - CS32Discussion Sec.on1A Week2 TA:BrianChoi...

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

View Full Document Right Arrow Icon
CS32 Discussion Sec.on 1A Week 2 TA: Brian Choi
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
Copy Constructors ‐ Mo.va.on class School { public: School(); School(const string &name); string getName() const; // accessor void setName(const string &name); // modifier void addStudent(const Student &student); // modifier Student *getStudent(const string &name) const; // accessor bool removeStudent(const string &name); // modifier int getNumStudents() const; // accessor private: string m_name; // name of the school Student *m_students; // dynamic array of students int m_numStudents; // number of students };
Image of page 2
Copy Constructors ‐ Mo.va.on Student st1(“Brian”); Student st2(“John”); School s1(“UCLA”); s1.addStudent(st1); s1.addStudent(st2); Student *p = s1.getStudent(“John”); s1 UCLA We want to create a new School called s2 , with exactly the same content as s1 .
Image of page 3

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

View Full Document Right Arrow Icon
Copy Constructors ‐ Mo.va.on Candidate 1: Just use an assignment School s2 = s1; What happens internally if you do this (assume no operator overloading)? What are the limits to this approach?
Image of page 4
Copy Constructors ‐ Mo.va.on Candidate 1 School s2 = s1; Every member variable gets copied – even the pointers (but not the pointees). It will first call the default constructor of s2, ini.alize members with default values, and then copy the values – efficiency issue . s1 s2 s1 UCLA UCLA UCLA
Image of page 5

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

View Full Document Right Arrow Icon
Copy Constructors ‐ Mo.va.on Candidate 2: Just grab values out of s1 and manually copy them into s2 School s2; s2.setName(s1.getName()); What are the limits to this approach?
Image of page 6
Copy Constructors ‐ Mo.va.on Candidate 2 School s2; s2.setName(s1.getName()); // how do I get students out of s1? We may not have accessors and modifiers to all member variables!
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
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