Inheritance in C CS1030 Dr Mark L Hornick

  • Slides: 21
Download presentation
Inheritance in C++ CS-1030 Dr. Mark L. Hornick 1

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

Inheritance Syntax l l l class Pet { protected: string name; public: int speak();

Inheritance Syntax l l l class Pet { protected: string name; public: int speak(); int eat(); } class Dog : public Pet {// Java uses extends private: bool has. Fleas; public: int speak(); // override } class Cat : public Pet { private: bool is. Declawed; public: int purr(); } Example Program… CS-1030 Dr. Mark L. Hornick 2

C++ Inheritance rules l Protected and public data and methods in the base class

C++ Inheritance rules l Protected and public data and methods in the base class are inherited by the derived class l With some exceptions: l l 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 has. Fleas; public:

Public/protected/private rules class Dog : <inheritance specifier> Pet { private: bool has. Fleas; public: int speak(); } <Inheritance specifier> is one of the following l public l l protected l l 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 l l l 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. l Base class data members are constructed

C++ Construction Order 1. 2. 3. 4. l 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 l Destructor order (reverse of ctor order) 1. 2. 3. 4. Derived

Destruction order l 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(); } l class

Method overrides class Pet { protected: string name; public: int eat(); } l class Dog : public Pet { private: bool has. Fleas; public: int eat(); // override replaces the inherited version int speak(); } l class Cat : public Pet { private: bool is. Declawed; public: int purr(); l } Example Program… CS-1030 Dr. Mark L. Hornick 7

Functional binding void main() { Dog spot; Dog: : speak() spot. speak(); Pet: :

Functional binding void main() { Dog spot; Dog: : speak() spot. speak(); Pet: : speak() Pet* pet = &spot; pet->speak(); Pet a. Pet; Pet: : speak() apet. speak(); } CS-1030 Dr. Mark L. Hornick 8

Functional Binding l Variant behavior l l Derived object – calls derived version Base

Functional Binding l Variant behavior l l Derived object – calls derived version Base pointer – calls base version This is inconsistent and not like Java This is called Early Binding l Functional version is chosen at compile time CS-1030 Dr. Mark L. Hornick 9

Polymorphism l The solution to variant behavior l l Version is chosen at run

Polymorphism l The solution to variant behavior l l Version is chosen at run time Delay the choice until we know the real type l Even when upcast! CS-1030 Dr. Mark L. Hornick 10

virtual Functions l Declaration in the base class l l l Forces use of

virtual Functions l Declaration in the base class l l l Forces use of late binding for a function Keyword: “virtual” Implementation l 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 :

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 Dog: : speak()

Calling virtual Functions void main() { Dog spot; spot. speak(); //virtual Dog: : speak() Pet* pet = &spot; pet->speak(); Pet a. Pet; Pet: : speak() apet. speak(); } CS-1030 Dr. Mark L. Hornick 13

General Overriding Rules l If you want variant behavior … l l l Don’t

General Overriding Rules l If you want variant behavior … l l l Don’t declare base class functions virtual Don’t override in derived classes invariant over specialization l l Create virtual base class function(s) Overrides expected, but not required in derived classes CS-1030 Dr. Mark L. Hornick 14

Abstract Base Classes l l Remember Java Abstract classes and Interfaces? Describes a set

Abstract Base Classes l l Remember Java Abstract classes and Interfaces? Describes a set form/template l Never intend to create an object l l Must have classes that implement the interface or abstract methods Compiler will not allow abstact classes/interfaces to be created l l 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 l l Done in C++

The C++ equivalent of Java abstract classes and interfaces l l Done in C++ by declaring pure virtual methods Language syntax virtual void speak()=0; // “ 0=pure virtual” l Not all methods have to be declared pure virtual l But having even one pure virtual method makes the entire class abstract CS-1030 Dr. Mark L. Hornick 16

Upcasting l Inheritance embodies “is a” l l Derived objects are base objects l

Upcasting l Inheritance embodies “is a” l l Derived objects are base objects l l l A derived object is a base object Dog spot; Pet* p. Pet = &spot; Always use pointers or references to do this l Otherwise the extra is “sliced” off: Pet a. Pet = spot; // ouch! Pet. speak(); // calls Pet: : speak() CS-1030 Dr. Mark L. Hornick 17

Object Slicing l Upcasting must be done carefully l l l Always use references

Object Slicing l Upcasting must be done carefully l l l Always use references or pointers So you have the original object Base copy constructors and operator= l l Cannot fully copy a derived object Slices off the “new” and overridden stuff CS-1030 Dr. Mark L. Hornick 18

virtual Destructors l Allows upcast objects to properly destroy l l l ALWAYS make

virtual Destructors l Allows upcast objects to properly destroy l l l ALWAYS make base class destructors virtual l l Forces use of the most derived destructor All the destructors are still called l Most derived base (in order) Good habit Pure virtual destructors must still be defined CS-1030 Dr. Mark L. Hornick 19

Polymorphic methods l Polymorphism is multiple versions of an object that: l l Change

Polymorphic methods l Polymorphism is multiple versions of an object that: l l Change their behavior based on context Adapt to the needs of the situation Primary example – upcast objects accessed via base class pointers Secondary example l l 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 :

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