lecture6 - Inheritance and Polymorphism

Is a technique that enables us to define a subclass

Info icon This preview shows pages 7–14. Sign up to view the full content.

View Full Document Right Arrow Icon
is a technique that enables us to define a subclass (like ShieldedRobot) and have it “inherit” all of the functions and data of a superclass (like Robot). We then add new methods/ data to the subclass, as needed. class Robot { public: void setX(int newX); int getX(); void setY(int newY); int getY(); private: int m_x, m_y; }; class ShieldedRobot is a kind of Robot { public: private: }; // ShieldedRobot has everything // a Robot does, plus : int getShield(); void setShield(int s); // a ShieldedRobot has x,y PLUS a int m_shield;
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
Inheritance class Robot { public: void setX(int newX) { m_x = newX; } int getX(); { return(m_x); } void setY(int newY) { m_y = newY; } int getY() { return(newY); } private: int m_x, m_y; }; class ShieldedRobot is a kind of Robot { public: // ShieldedRobot has everything // a Robot does, plus: void setShield(int s) { m_shield = s; } int getShield() { return(m_shield); } private: // a ShieldedRobot has x,y PLUS a int m_shield; }; How it works: You define the superclass and implement all of its member functions. Then you define your subclass, based on the superclass , adding new variables and member functions as needed.
Image of page 8
Inheritance class Robot { public: void setX(int newX) { m_x = newX; } int getX(); { return(m_x); } void setY(int newY) { m_y = newY; } int getY() { return(newY); } private: int m_x, m_y; }; class ShieldedRobot is a kind of Robot { public: // ShieldedRobot has everything // a Robot does, plus: void setShield(int s) { m_shield = s; } int getShield() { return(m_shield); } private: // a ShieldedRobot has x,y PLUS a int m_shield; }; void main(void) { ShieldedRobot r; r.setX(5); r.setY(6); r.setShield(10); ... r Robot data: m_x: m_y: ShieldedRobot data: m_shield: C++ automatically determines which function to call… 5 5 6 6 10 10
Image of page 9

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

View Full Document Right Arrow Icon
Inheritance Just as a Student is a person (plus an ID#), a ShieldedRobot is a Robot (plus a shield strength). Any time we have such a relationship: “A is a type of B”, C+ + inheritance may be warranted. In contrast, consider a Person and a name . A person has a name, but you wouldn’t say that “a person is a name .” See the difference between Student & Person vs. Person & name ? class Person { public: string getName(void); void setName(string & n); int getAge(void); void setAge(int age); private: string m_sName; int m_nAge; };
Image of page 10
Inheritance Animal Fish Mammal Primate Marsupial Reptile Ape Human “is a” “A mammal is an animal (with fur)” “is a” “A marsupial is a mammal (with a pouch)” This is called a Class Hierarchy
Image of page 11

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

View Full Document Right Arrow Icon
Inheritance: Terminology Animal Fish Mammal Primate Marsupial Reptile Animal is called a superclass (also called a base class ). Mammal is called a subclass (also called a derived class ) of Animal , and a superclass of Primate and Marsupial . Primate is a subclass (also called a derived class ) of Mammal.
Image of page 12
Inheritance In C++, you can inherit more than once: class Person { public: string getName(void); ... private: string m_sName; int m_nAge; }; class Student is a kind of Person { public: // new stuff: int GetStudentID(); ...
Image of page 13

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

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