lecture6 - Inheritance and Polymorphism

Mx my 0 inheritance and member visibility a derived

This preview shows 28 out of 36 pages.

m_x = m_y = 0; / / ????
Image of page 28

Subscribe to view the full document.

Inheritance and Member Visibility A derived class may only access the public members of the base class . All private members of the base class are hidden from the derived class and may only be accessed via the public interface of the base class. All mammals have a spleen (which is hidden/ private). A human is a mammal. Can you access/ touch your spleen? (That’s the most rigorous proof I’ll give this quarter!)
Image of page 29
Inheritance and Member Visibility If you would like your derived class to be able to access private member variables of the base class… But you don’t want the rest of your program to access these member variables… Then make them protected instead of private : class Robot { public: Robot(void); void setX(int newX); int getX(); ... private : int m_x, m_y; }; class ShieldedRobot : public Robot { public: ShieldedRobot(void) { m_shield = 1; m_x = m_y = 0; // ERROR! } void setShield(int s); ... private: int m_shield; }; / / It’s OK main() { ShieldedRobot x(1); x.m_x = 5; } protected: / / STILL AN ERROR!
Image of page 30

Subscribe to view the full document.

Inheritance & Assignment Ops What happens if I assign one instance of a derived class to another? class Robot { public: void setX(int newX); int getX(); void setY(int newY); int getY(); private: int m_x, m_y; }; main() { ShieldedRobot larry, curly; larry.setShield(5); larry.setX(12); larry.setY(15); curly.setShield(75); curly.setX(7); curly.setY(9); larry = curly; // what happens? } class ShieldedRobot: public Robot { public: int getShield (); void setShield(int s); private: int m_shield; };
Image of page 31
Inheritance & Assignment Ops It works fine. main() { ShieldedRobot larry, curly; larry = curly; // hmm? } However , if any of your classes have dynamically allocated member variables (or would otherwise need a special copy constructor/ assignment operator)… then you must define assignment ops and copy ctors for that class and all classes derived from it. larry Robot data: m_x: m_y: ShieldedRobot data: m_shield: 12 15 5 curly Robot data: m_x: m_y: ShieldedRobot data: m_shield: 7 9 75 C++ first copies the base data, from curly to larry, and then copies the derived data from curly to larry.
Image of page 32

Subscribe to view the full document.

Inheritance & Assignment Ops class Person { public: Person() { myBook = new Book; } / / I allocate memory!!! Person(const Person & other); Person& operator=(const Person & other); private: Book *myBook; }; class Student: public Person { public: Student(const Student & other) : Person(other) { myGPA = other.myGPA; } Student& operator=(const Student & other) { Person::operator=(other); myGPA = other.myGPA; return(*this); } private: double myGPA; };
Image of page 33
class Person { public: string getName(void); ... private: string m_sName; int m_nAge; }; Polymorphism Any time you have a function that takes a reference to a superclass as an argument... void SayHi( Person &p ) { cout << “Hello “ << p.getName(); } main() { } Person c; SayHi( c ); You may also pass in a subclass to the function. Student s; SayHi( s ); class Student : public Person { public: // new stuff: int getStudentID(); private: // new stuff: int m_nStudentID; }; Person Student
Image of page 34

Subscribe to view the full document.

Polymorphism Why is this? Well a Student IS a Person. Everything a Person can do, it can do.
Image of page 35
Image of page 36
You've reached the end of this preview.

{[ 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