COM SCI 32 Lecture Notes - Lecture 7: Virtual Function, Batmobile, Member Variable

109 views6 pages

Document Summary

Cs 32 - lec 7 - inheritance & polymorphism. Inheritance: reuse: reusing code to save time, extension: adding new behaviors(member functions) or data. Batmobile: public car -> void shootlaser(float angle: specialization/overriding: redefine an existing base class behavior. *insert the virtual keyword in front of both the original and replacement functions. A student is a type of person //can use inheritance. A person has a name //make name a member variable. C++ constructs the base then the derived part. C++ destructs the derived part then the base. Animal (int lbs) // ***since this constructor requires parameters m_lbs = lbs; virtual void whatdoisay() cout << i am an animal. //derived class class duck : public animal //inheritance public: // ***then you must use an initializer list here. : animal(lbs) m_feathers = numf; virtual void whatdoisay() //virtual: specialization/overriding cout << i am a duck! ; //uses original whatdoisay() private: int m_feathers; class mallard : public duck { public:

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents