240 likes | 680 Views
Inheritance in C++. 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 }
E N D
Inheritance in C++ CS-1030 Dr. Mark L. Hornick
Inheritance Syntax • class Pet {protected: string name; public: int speak(); int eat(); } • class Dog : public Pet {// Java uses extendsprivate: bool hasFleas;public: int speak(); // override} • class Cat : public Pet {private: bool isDeclawed;public: int purr();} Example Program… CS-1030 Dr. Mark L. Hornick
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
Public/protected/private rules class Dog : <inheritance specifier> Pet {private: bool hasFleas;public: int speak();} <Inheritance specifier> is one of the following • public • 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 • protected • 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
C++ Construction Order • 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…
Destruction order • Destructor order (reverse of ctor order) • 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
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
Dog::speak() Pet::speak() Functional binding void main() { Dog spot; spot.speak(); Pet* pet = &spot; pet->speak(); Pet aPet; apet.speak(); } Pet::speak() CS-1030 Dr. Mark L. Hornick
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
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
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
virtual is optional here virtual Example class Pet { public: virtual void speak(); … }; class Dog : public Pet { public: void speak(); … }; CS-1030 Dr. Mark L. Hornick
Dog::speak() Dog::speak() Calling virtual Functions void main() { Dog spot; spot.speak(); //virtual Pet* pet = &spot; pet->speak(); Pet aPet; apet.speak(); } Pet::speak() CS-1030 Dr. Mark L. Hornick
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
Abstract Base Classes • Remember Java Abstractclasses 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
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
Upcasting • Inheritance embodies “is a” • A derived object is a base object • Derived objects are base objects • 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
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
virtual Destructors • Allows upcast objects to properly destroy • Forces use of the most derived destructor • All the destructors are still called • Most derived base (in order) • ALWAYS make base class destructors virtual • Good habit • Pure virtual destructors must still be defined CS-1030 Dr. Mark L. Hornick
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
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