1 / 21

Inheritance in C++

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 }

etoile
Download Presentation

Inheritance in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance in C++ CS-1030 Dr. Mark L. Hornick

  2. 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

  3. 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

  4. 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

  5. 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…

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

More Related