COM SCI 32 Lecture Notes - Lecture 8: In C, Virtual Function, Batmobile
Document Summary
Inheritance is a way to form new classes using classes that have already been identi(cid:210)ed. Inheritance is the basis of all objected oriented programming. Let"s consider the robot class: class robot public: void setx(int newx); int getx(); void sety(int newy); int gety(); private: int m_x, m_y; Now let"s consider the 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; Both classes have x and y coordinates and provide the same set of methods to get and set the values of x and y. A shielded robot is a kind of robot! It shares all of the same methods and data as a robot. C++ lets us de(cid:210)ne a new class and have it inherit all of the methods/data of an existing, related class.