{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 6 - Inheritance

Lecture 6 - Inheritance - Wednesday,January25th Inheritance...

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

View Full Document Right Arrow Icon
Wednesday, January 25th Inheritance  From Wikipedia: Inheritance is a way to form new classes (instances of which are called objects) using classes that  have already been defined. 
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
Inheritance Let’s say we’re writing a video game.  In the game, the player has to fight various  monsters  and save the world.  For each monster you could provide  a class definition. class Robot { public: void setX(int newX); int getX(); void setY(int newY); int getY(); private: int m_x, m_y; }; For example, consider the  Robot   class: 
Image of page 2
Inheritance Now lets consider a  Shielded Robot  class: class ShieldedRobot { public: void setX(int newX); int getX(); void setY(int newY); int getY(); int getShield (); void setShield(int s); private: int m_x, m_y, m_shield; }; Let’s  compare both classes… What are  their similarities? class Robot { public: void setX(int newX); int getX(); void setY(int newY); int getY(); private: int m_x, m_y; };  Both classes have  x  and  y     coordinates   In class  Robot ,   x  and  y  describe    the position of the robot  In class  ShieldedRobot  they also     describe the robot’s position   So  x  and  y  have the same     meaning in both classes:  They     describe the position of the     robot  Both classes also provide the     same set of methods to  get  and       set  the values of  x  and  y
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
Inheritance class ShieldedRobot { public: void setX(int newX); int getX(); void setY(int newY); int getY(); int getShield(); void setShield(int s); private: int m_x, m_y, m_shield; }; class Robot { public: void setX(int newX); int getX(); void setY(int newY); int getY(); private: int m_x, m_y; };   In fact, the only difference    between a  Robot  and a  ShieldedRobot  is that a  ShieldedRobot  also has a  shield  to protect it. int m_x, m_y, m_shield ; It’s a pity that even though  ShieldedRobot   has just a  few extra features  we have to  define a  whole new class  for it! ShieldedRobot  essentially  is a   Robot !  A  ShieldedRobot shares all of the same functions  and data as a Robot; it just has some  additional  methods/data.
Image of page 4
Inheritance Here’s another example: class Person { public: string getName(void); void setName(string & n); int getAge(void); void setAge(int age); private: string m_sName; int m_nAge; }; class Student { public: string getName(void); void setName(string & n); int getAge(void); void setAge(int age); int getStudentID(); void setStudentID(); float getGPA(); private: string m_sName; int m_nAge; int m_nStudentID; float m_fGPA; };
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
Inheritance Student  essentially  is a   Person !  A Student shares all of  the same functions and data as a Person; it just has some  additional  methods/data.
Image of page 6
Image of page 7
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