Inheritance in C++ CS-1030 Dr. Mark L. Hornick 1 Inheritance Syntax class Pet { protected: string name; public: int speak(); int eat(); } class Dog : public Pet {// Java uses extends private: bool hasFleas; public: int speak(); // override } class Cat : public Pet { private: bool isDeclawed; public: int purr(); } Example Program… CS-1030 Dr. Mark L. Hornick 2 C++ Inheritance rules Protected and public data and methods in the base class are inherited by the derived class With some exceptions: Constructors Destructors operator= Are accessible in the derived class. CS-1030 Dr. Mark L. Hornick 3 Public/protected/private rules class Dog : <inheritance specifier> Pet { private: bool hasFleas; public: int speak(); } <Inheritance specifier> is one of the following public protected All public and protected members of Pet will be considered public or protected within Dog Still inherits Pet’s private members, but they’re not accessible from Dog methods Also inherits Pet’s protected members, and they’re accessible from Dog methods All public and protected members of Pet will be considered protected within Dog What was public in Pet is protected within Dog Still inherits Pet’s private members, but they’re not accessible from Dog methods private All public and protected members of Pet will be considered private within Dog What was public or protected in Pet is private within Dog Still inherits Pet’s private members, but they’re not accessible from Dog methods CS-1030 Dr. Mark L. Hornick 4 C++ Construction Order 1. 2. 3. 4. Base class data members are constructed in order of declaration Base class constructor is called Derived class data members are constructed in order Derived class constructor is executed This is much different from Java, where super() has to be invoked from the derived class constructor to call the base class constructor CS-1030 Dr. Mark L. Hornick Example Program… 5 Destruction order Destructor order (reverse of ctor order) 1. 2. 3. 4. Derived class destructor is executed Derived class data members are destroyed The base class destructor is called Base class data members are destroyed CS-1030 Dr. Mark L. Hornick 6 Method overrides class Pet { protected: string name; public: int eat(); } class Dog : public Pet { private: bool hasFleas; public: int eat(); // override replaces the inherited version int speak(); } class Cat : public Pet { private: bool isDeclawed; public: int purr(); } Example Program… CS-1030 Dr. Mark L. Hornick 7 Functional binding void main() { Dog spot; spot.speak(); Dog::speak() Pet* pet = &spot; Pet::speak() pet->speak(); Pet aPet; Pet::speak() apet.speak(); } CS-1030 Dr. Mark L. Hornick 8 Functional Binding Variant behavior Derived object – calls derived version Base pointer – calls base version This is inconsistent and not like Java This is called Early Binding Functional version is chosen at compile time CS-1030 Dr. Mark L. Hornick 9 Polymorphism The solution to variant behavior Version is chosen at run time Delay the choice until we know the real type Even when upcast! CS-1030 Dr. Mark L. Hornick 10 virtual Functions Declaration in the base class Forces use of late binding for a function Keyword: “virtual” Implementation Compiler inserts code to ask the object before calling a virtual function CS-1030 Dr. Mark L. Hornick 11 virtual Example class Pet { public: virtual void speak(); … }; class Dog : public Pet { public: void speak(); … }; virtual is optional here CS-1030 Dr. Mark L. Hornick 12 Calling virtual Functions void main() { Dog spot; spot.speak(); //virtual Pet* pet = &spot; Dog::speak() Dog::speak() pet->speak(); Pet aPet; Pet::speak() apet.speak(); } CS-1030 Dr. Mark L. Hornick 13 General Overriding Rules If you want variant behavior … Don’t declare base class functions virtual Don’t override in derived classes invariant over specialization Create virtual base class function(s) Overrides expected, but not required in derived classes CS-1030 Dr. Mark L. Hornick 14 Abstract Base Classes Remember Java Abstract classes and Interfaces? Describes a set form/template Never intend to create an object Must have classes that implement the interface or abstract methods Compiler will not allow abstact classes/interfaces to be created References are OK (In C++, Pointers are OK too) CS-1030 Dr. Mark L. Hornick 15 The C++ equivalent of Java abstract classes and interfaces Done in C++ by declaring pure virtual methods Language syntax virtual void speak()=0; // “0=pure virtual” Not all methods have to be declared pure virtual But having even one pure virtual method makes the entire class abstract CS-1030 Dr. Mark L. Hornick 16 Upcasting Inheritance embodies “is a” Derived objects are base objects A derived object is a base object Dog spot; Pet* pPet = &spot; Always use pointers or references to do this Otherwise the extra is “sliced” off: Pet aPet = spot; // ouch! Pet.speak(); // calls Pet::speak() CS-1030 Dr. Mark L. Hornick 17 Object Slicing Upcasting must be done carefully Always use references or pointers So you have the original object Base copy constructors and operator= Cannot fully copy a derived object Slices off the “new” and overridden stuff CS-1030 Dr. Mark L. Hornick 18 virtual Destructors Allows upcast objects to properly destroy ALWAYS make base class destructors virtual Forces use of the most derived destructor All the destructors are still called Most derived base (in order) Good habit Pure virtual destructors must still be defined CS-1030 Dr. Mark L. Hornick 19 Polymorphic methods Polymorphism is multiple versions of an object that: Change their behavior based on context Adapt to the needs of the situation Primary example – upcast objects accessed via base class pointers Secondary example Operators: + addition, concatenation (later) Functions: Multiple constructors CS-1030 Dr. Mark L. Hornick 20 Polymorphic method Example int Max(int a, int b) { return (a<b) ? b : a;} double Max(double a, double b) { return (a<b) ? b : a;} Complex Max(const Complex& a, const Complex& b) { return (a<b) ? b : a;} All the functions are the same! CS-1030 Dr. Mark L. Hornick 21