� ������������������������������������������������������� � ������������������������������������������������� Advanced C++ Topics Inheritance Revisited � Inheritance is useful to � Inheritance � explicitly represent relationships among program � Virtual methods and late binding components � reuse as much design and implementation effort as � Friend classes and methods possible � Class templates � avoid parallel implementations that are error prone since they are hard to keep synchronized � Overloaded operators � Class hierarchies represent shared and distinct � Iterators relationships between classes � derived (sub) class inherits base (super) class properties � all member data and functions except constructors and destructors EECS 268 Programming II 1 2 Inheritance � Basic Concepts Inheritance � Basic Concepts � ������������������������������������������������ � Superclass or base class � An instance of the base class � a class from which another class is derived � An instance of the derived class � Subclass, derived class, or descendant class � ��������������������������� � a class that inherits all members of another class � can add new members to those it inherits members (except constructors and destructor) � An instance of a derived class has all the behaviors of � can redefine an inherited method of its base class, ��������������������������������������������������������� if the two methods have the same parameter declarations data and methods directly by name EECS 268 Programming II 3 EECS 268 Programming II 4
Inheritance Revisited Inheritance � Syntax � class derivedClass: access-modifier baseClass � access modifier describes access semantics of base class components inherited by derived class � Public methods can be used by any code � client, class member functions, derived classes � Private members � class member functions and friends � Protected � class members, friends, derived classes EECS 268 Programming II 5 EECS 268 Programming II 6 Kinds of Inheritance Inheritance Hierarchy � Example � Apply most restrictive access based on base access type and inheritance access modifier � Public inheritance � Public/Protected � Public/Protected derived members � Protected inheritance � Public/Protected � Protected derived members � Private Inheritance � Public/Protected � Private derived members � Private base class members remain private under all inheritance types Figure 8-1 Inheritance: Relationships among timepieces EECS 268 Programming II 7 8
� ��������������������������������������������� Inheritance � Example Multiple Inheritance � Sphere serves as a base class for Ball � Multiple inheritance � some routines inherited, some new, and some are � a derived class can have more than one base class redefined (e.g. display Statistics()) � we will not study this kind of inheritance 9 EECS 268 Programming II 10 Inheritance Revisited Is-a Relationships � Public inheritance should imply an is-a relationship � Object type compatibility private � you can use an instance of a derived class anywhere you can use an instance of the base class (but not the other way around) � Example � A ball is a sphere � Given the following function declaration: void displayDiameter(Sphere thing); The following statements are valid: Ball myBall �������������������� displayDiameter(myBall); see C8-Sphere.cpp, C8-Ball.cpp 11 EECS 268 Programming II 12
� ������������������������������������������������������������ Sphere/Ball Example Sphere/Ball Example � 2 � Ball class � Ball class (derived from Sphere) � displayStatistics() redefines the name in the � both constructors call base class constructors to derived class as a local method handle private radius data � ��������������������������������������� theName � � getName() is a new method � uses full class::member scope resolution syntax to call � setName() gives access to Ball data ��������� displayStatistics() � instance of Ball has two data members � resetBall() uses both Sphere and Ball access � theName and theRadius (inherited) routines as the data is private in both classes � since Sphere::theRadius is defined private (rather than � Ball could access name directly protected or public) it can only be accessed through the public (get/set)Radius() methods EECS 268 Programming II 13 EECS 268 Programming II 14 Has-a Relationships As-a Relationships � If the relationship between two classes is not is-a, � Uses private inheritance do not use public inheritance � Example: Implement a stack as a list � Has-a relationship (also called containment ) class Stack: private List � a class has an object as a data member � cannot be implemented using inheritance methods � the underlying list is hidden from the clients and � Example: A has-a relationship between a pen and descendants of the stack a ball � Private inheritance is useful when class Pen { � � a class needs access to the protected members of private: another class, or Ball point; }; � if methods in a class need to be redefined EECS 268 Programming II 15 EECS 268 Programming II 16
As-a relationship � Stack as-a List Example Private Inheritance � A stack is not a type a list since it has unique � All public, protected and private elements of semantics the base class are private in the derived class. � Stack semantics can be implemented in terms on � Client code reference to a public base class List semantics routine through the derived class instance is � Private inheritance strongly conceals any list illegal. related semantics from the clients � Derived class completely wraps base class � only exposes push()/pop() elements. � cannot access insert()/remove() � Contrast with the Stack has-a List implementation � Derived class is implemented using or is from Chapter 4 implemented in terms of the base class. � just as good; both work see C8-StackL.cpp, C8-List.cpp EECS 268 Programming II 17 EECS 268 Programming II 18 Inheritance & Class Relationships Class Relationships � Public inheritance � IS-A: derived class is special kind of base class � extend or specialize an existing class � public inheritance used to implement in C++ � most common � AS-A: derived class implemented in terms of � is-a relationship between base/derived classes base class � Protected inheritance � private inheritance can be used in C++ � not very useful; not often used � HAS-A: object A includes instance of object B � Private inheritance as part of its implementation � to implement one class in terms of another � encapsulation is just as good as inheritance � as-a relationship EECS 268 Programming II 19 EECS 268 Programming II 20
Virtual Functions Virtual Functions � 2 class Animal { � Base class Animal gives � Derived class sometimes need to modify or completely public: replace actions of a base class method permission to override ... � derived class is said to override the inherited method virtual void breathe( ); // uses a nose breathe() and move() virtual void move(); // uses feet � Base class must give permission for redefinition � Derived class Fish over- ... � by declaring the method as virtual }; rides both, but denies � Redefinition is permitted but not required class Fish: public Animal { permission to further public: � Redefined methods must have exactly the same ... override breathe() signature as the inherited base class methods void breathe( ); // uses gills � Derived functions do not need to use the virtual virtual void move(); // uses fins � WalkingCatFish over- keyword ... rides move() but uses } � Friend and constructor functions cannot be virtual, but class WalkingCatFish: public Fish { inherited breathe() destructors can be void move(); // uses fins as feet } EECS 268 Programming II 21 EECS 268 Programming II 22 Function Overriding Example Name Binding Time class secondClass : class baseClass{ � Binding time can be compile-time or run-time public baseClass{ public: � controls what methods are called in some cases }; virtual void print(){ � see C8-staticBinding2.cpp �������������������� class thirdClass : } public baseClass{ � Direct access using b and d instance variable }; public: obviously compile time void print(){ class firstClass : � But what if they point to a different type ��������������������� public baseClass{ } object?? public: }; � bp1 is legal (but bogus) void print(){ class fourthClass : ��������������������� � dp2 illegal type conversion public thirdClass{ } }; }; see C8-staticBinding.cpp EECS 268 Programming II 23 EECS 268 Programming II 24
Recommend
More recommend