lecture6

lecture6 - Wednesday, J anuar y 21 I nher i tance Pol ymor...

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

View Full Document Right Arrow Icon
Wednesday, January 21 st Inheritance Polymorphism Proof that 1 = 2: Assume a = b Then ab = a 2 = b 2 So ab + (a 2 – 2ab) = a 2 + (a 2 – 2ab) Simplifying , a 2 – ab = 2a 2 – 2ab Factoring , 1(a 2 – ab) = 2(a 2 – ab) What’s wrong with this proof?
Background image of page 1

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

View Full DocumentRight 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. class Robot { public: void setX(int newX); int getX(); void setY(int newY); int getY(); private: int m_x, m_y; }; the Robot class:
Background 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 y describe the position of the robot ShieldedRobot they also describe the robot’s position So x 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 set the values of x y
Background image of page 3

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

View Full DocumentRight 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! A ShieldedRobot essentially is a ! A ShieldedRobot shares all of the same functions and data as a Robot; it just has some additional methods/ data.
Background 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; };
Background image of page 5

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

View Full DocumentRight Arrow Icon
Inheritance A Student essentially is a Person ! Student shares all of the same functions and data as a Person; it just has some additional methods/ data. and are so closely related… Yet, to define a , I have to rewrite every one of the functions from in my class. What a waste of time! 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; };
Background image of page 6
Inheritance 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).
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 36

lecture6 - Wednesday, J anuar y 21 I nher i tance Pol ymor...

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

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