Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance. Today: Inheritance and derived classes Is-A relationship class hierarchies proper inheritance (pure) polymorphism virtual functions protected.

Similar presentations


Presentation on theme: "Inheritance. Today: Inheritance and derived classes Is-A relationship class hierarchies proper inheritance (pure) polymorphism virtual functions protected."— Presentation transcript:

1 Inheritance

2 Today: Inheritance and derived classes Is-A relationship class hierarchies proper inheritance (pure) polymorphism virtual functions protected members inheritance and constructors/destructors

3 Inheritance and Derived Classes l Problem domain objects are related l Has-A relationship a Car has: Engine, Transmission, Suspension etc. usually modeled in C and C++ as composition classes/structs have other structs as members l Another relationship: Is-A (also known as Kind-Of specialization/generalization

4 Example of an Is-A relationship l Employee name, hiring date, department l Manager same behavior (services and state) as employee, plus: employees managed; level

5 Implementing Is-A in C struct Employee { name, dept, etc } struct Manager { Employee emp; /* containment */ Employee managed[25]; unsigned level; } l Implemented Is-A as Has-A (containment)

6 Problems with Is-A implementation l Even though a Manager “object” has everything an Employee has plus more… Cannot pass a Manager variable where Employee is expected Same for pointers: Manager* and Employee* What about a mid-level Manager?

7 Modeling Is-A in C++: Public Inheritance class Manager : public Employee { // data and methods // unique to Manager } l Employee - a base class l Manager - a derived class

8 Inheriting methods and data class Employee { get/setName(); … } Employee e; Manager m; cout << e.getName(); cout << m.getName();

9 Inheriting methods and data All Employee public members and methods are retained (inherited) by Manager Employee public methods can be used by Manager as if it were an Employee easy reuse of code l Constructors, destructors, operator= are not inherited

10 Access rules for derived classes l (Public inheritance only) l public data and functions remain public l private members are not accessible in derived classes. l Why? Trivial to defeat encapsulation by deriving a class

11 Class hierarchies l Typical to discover new relationships between domain objects/new specialized objects class Director : public Manager { Car corporateVehicle; … }

12 Class hierarchies Director Employee Manager Is-A special kind of Employee Is-A special kind of Manager Base type

13 Class hierarchies Director Employee ManagerTemporary

14 Class hierarchies l Typically “root” object at the top - inverted tree

15 Proper Inheritance l When is a relationship Is-A? class Rectangle {... } class Square : public Rectangle {... }

16 Proper Inheritance class Rectangle { // methods to set height and width } class Square : public Rectangle { // method to set size } Cannot call setHeight()/setWidth() on a Square

17 Proper Inheritance l Criterion: substitutability An object of class Derived can be substituted for an object of class Base everywhere Not true for Rectangle(Base) and Square (Derived)

18 Three options allow Square to have different width and height do not guarantee that setWidth()/setHeight work on all Rectangle l drop the inheritance relationship

19 Improper inheritance: another example class Bird { void fly(); … } class Penguin : public Bird { // cannot fly }

20 Improper inheritance: general case l A base class with an “extra” capability a derived class can't satisfy. l Three options: make the base class weaker make the derived class stronger eliminate the proposed inheritance relationship

21 Bad way of dealing with improper inheritance l Attempt to determine actual type of object at run-time: if (a shape is actually a Rectangle) process it as a rectangle elseif (a shape is a Square) do not attempt to change W and H separately else ???

22 Polymorphic class pointers Employee* eptr; Employee e; Manager m; eptr = &e; eptr = &m; l Similar for references

23 Polymorphic pointers and references l A pointer or reference of a Base class can always refer to an object of a Derived class (because a Derived Is-A Base) l But not vice versa

24 Virtual functions Employee::print() print name, department Manager::print() print Employee information + level

25 Virtual functions class Employee { virtual void print(); … } class Manager { virtual void print(); … }

26 Virtual functions Employee* eptr; Employee e; Manager m; eptr = &e; eptr->print(); // Employee::print eptr = &m; eptr->print(); // Manager::print

27 Virtual functions l Actual function called depends on the dynamic type of the pointer l Static vs. dynamic types l Virtual functions use late binding

28 Alternative: explicit type field class Employee { enum EmplType { E, M }; EmplType type; void print(); … } switch (e->type) { … }

29 Virtual functions: details l Signatures of virtual functions in base and derived class must exactly match

30 Prohibiting virtual function resolution Manager::print() { // print Manager-specific stuff // explicitly call print() from // base class Employee::print(); }

31 Inheritance and constructors l Objects are constructed “bottom-up” base first then member objects finally, derived class l Constructor in derived class must call constructor(s) for base and member classes

32 Inheritance and destructors l Destructor in Derived automatically calls destructor in Base

33 Summary l Is-A relationship l Access inheritance l class hierarchies l proper inheritance l Virtual functions Static vs. dynamic type l Inheritance and constructors/destructors


Download ppt "Inheritance. Today: Inheritance and derived classes Is-A relationship class hierarchies proper inheritance (pure) polymorphism virtual functions protected."

Similar presentations


Ads by Google