lecture6 - Inheritance and Polymorphism

Void studentwhatdoisay cout hello class nerdystudent

Info icon This preview shows pages 18–25. Sign up to view the full content.

View Full Document Right Arrow Icon
}; void Student::WhatDoISay() { cout << “Hello!”; } class NerdyStudent: public Student { public: virtual void WhatDoISay(); ... }; void NerdyStudent::WhatDoISay() { cout << “I love circuits!”; } Use virtual here within your class definition: Don’t write virtual here:
Image of page 18

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

View Full Document Right Arrow Icon
Inheritance: Function Call Rules class Student { public: virtual void cheer () { cout << “go bruins!”; } void goToBathroom () { cout << “splat!”; } ... }; class NerdyStudent: public Student { public: virtual void cheer () { cout << “go algorithms!”; } ... }; Rules: 1. If you define a function in the base class AND in the derived class, then the derived version of the function will hide the base version of the function . main() { NerdyStudent JR; JR.cheer(); } go algorithms!
Image of page 19
Inheritance: Function Call Rules main() { NerdyStudent CN; CN.goToBathroom(); CN.getExcitedAboutCS(); } Rules: 1. Any public functions defined in the base class, but not redefined in a derived class can be called normally by all parts of your program. class Student { public: virtual void cheer () { cout << “go bruins!”; } void goToBathroom () { cout << “splat!”; } ... }; class NerdyStudent: public Student { public: virtual void cheer () { cout << “go algorithms!”; } void getExcitedAboutCS() { cout << “Ooh linked lists!”; goToBathroom(); } }; splat! Ooh linked lists! splat!
Image of page 20

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

View Full Document Right Arrow Icon
Inheritance: Function Call Rules main() { NerdyStudent Lily; Lily.getExcitedAboutCS(); } 1. If your code wants to call a function in the base class that’s been redefined in the derived class, it must use a class:: prefix. Rules: class Student { public: virtual void cheer () { cout << “go bruins!”; } void goToBathroom () { cout << “splat!”; } ... }; class NerdyStudent: public Student { public: virtual void cheer () { cout << “go algorithms!”; } void getExcitedAboutCS() { cheer(); Student:: cheer(); } }; Lily.Student::cheer(); go algorithms! go bruins! go bruins! Ahh, since the programmer prefixed this with Student:: I’ll call its version of the cheer function!
Image of page 21
Inheritance & Construction // superclass class Robot { public: Robot(void) { m_x = 0; m_y = 0; } ~Robot() { … } void setX(int newX); int getX(); void setY(int newY); int getY(); private: int m_x, m_y; }; // subclass class ShieldedRobot: public Robot { public: ShieldedRobot(void) { m_shield = 1; // shield power } ~ShieldedRobot() { ... } void setShield(int s); int getShield(); private: int m_shield; }; main() { ShieldedRobot x; ... } Classes are constructed from superclass to subclass … And destructed from sub to super . x Robot data: m_x: m_y: ShieldedRobot data m_shield: 0 0 1 / / x d’tor
Image of page 22

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

View Full Document Right Arrow Icon
Inheritance & Construction main() { Animal a( 10 ); // 10 lbs a.what_do_i_weigh(); } Consider the following base class: Animal When you construct an Animal , you must specify the animal’s weight. class Animal // base class { public: Animal(int lbs) {m_lbs = lbs;} void what_do_i_weigh(void) {cout << m_lbs << lbs!\n"; } private: int m_lbs; };
Image of page 23
Inheritance & Construction class Animal // base class { public: Animal(int lbs) {m_lbs = lbs;} void what_do_i_weigh(void) {cout << m_lbs << lbs!\n"; } private: int m_lbs; }; Now consider the Duck class . It’s a subclass of Animal.
Image of page 24

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

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