460 likes | 475 Views
Learn about the concepts of class inheritance, including is-a relationships, access specifiers, initializer lists, upcasting and downcasting, virtual member functions, and more in C++. Includes example code.
E N D
Class Inheritance • Inheritance as an is-a relationship • Public derive one class from another • Protected access • Initializer lists in constructor • Upcasting and downcasting • Virtual member functions • Early binding and late binding • Abstract base classes • Pure virtual function • Public inheritance
Inheriting classes • Base class • Derived class • Program tabtenn0.h, tabtenn0.cpp, usett0.cpp
Derived class class RatedPlayer : public TableTennisPlayer { private: int rating; public: … };
Initializer list RatedPlayer::RatedPlayer(unsigned int r, const char *fn, const char*ln, bool ht): TableTennisPlayer(fn, ln, ht) { rating = r; } … RatedPlayer rplayer1(1140, “Mallory”, “Duck”, true);
Initializer list RatedPlayer::RatedPlayer(unsigned int r, const char *fn, const char*ln, bool ht): TableTennisPlayer(fn, ln, ht), rating(r) { } … RatedPlayer rplayer1(1140, “Mallory”, “Duck”, true);
Constructor for derived class • The base-class object is constructed first • The derived-class constructor should pass base-class information to a base-class constructor via a member initializer list • The derived-class constructor shoud initialize the data members that were added to the derived class
Using a derived class • Program tabtenn1.h, tabtenn1.cpp, usett1.cpp
Special relationship between base and derived class • Derived class uses base class methods • Upward casting is allowed • Downward casting is not safe
Implicit upward casting void Show(const TableTennisPlayer &rt) { cout << “Name: ”; rt.Name(); cout << “\nTable: ”; if (rt.HasTable()) cout << “yes\n”; else cout << “no\n”; } TableTennisPlayer player1(“Tara”, “Boomdea”, false); RatedPlayer rplayer1(1140, “Mallory”, “Duck”, true); Show(player1); Show(rplayer1);
Implicit copy constructor RatedPlayer olaf1(1190, “Olaf”, “Loaf”, true); TableTennisPlayer olaf2(olaf1), olaf3; // invoke implicit copy constructor and upward casting // TableTennisPlayer(const TableTennisPlayer&); TableTennisPlayer winer = olaf1; TableTennisPlayer winer = TableTennisPlayer(olaf1); olaf3 = olaf1;//assignment op and upward casting
Inheritance model • Public • An is-a relationship • Illegal relationships • Is-like-a, has-a, is-implemented-as, uses-a • Private • Protected
Polymorphic public inheritance • Redefining base-class methods in a derived class • Using virtual methods • Program brass.h, brass.cpp, usebrass1.cpp • Virtual method behavior and the need for virtual destructors in base class • Program usebrass2.cpp
Bindings • Static binding (early binding) during compiling • Dynamic binding (late binding) during execution
Pointer and reference type compatibility double x = 2.5; int *pi = &x;// invalid, mismatch pointer type long & r = x;// invalid, mismatch reference type BrassPlus dilly(“Annie Dill”, 34854, 3000); Brass *pd = &dilly; // ok Brass &rd = dilly; // ok
Upcasting & downcasting • Upcasting • Converting a derived-class reference or pointer to a base-class reference or pointer • Implicit type cast is allowed • Is–a relationship • Downcasting • Converting a base-class pointer or reference to a derived-class pointer or reference • Only allow explicit type cast
Virtual member functions and dynamic binding • Virtual function uses dynamic binding, whereas, non-virtual function employs static binding
Virtual or non-virtual? • Virtual function • Base class member function is allowed to be redefined in derived class • Non-virtual function • Base class member function is not allowed to be redefined in derived class
Why two kinds of binding?Why static is the default? • Two reasons • Efficiency – static binding is a little efficient than dynamic binding • The conceptual model – if you don’t want to redefine member functions in derived classes
How virtual function work? • Virtual function table (vtbl) Scientist sophie(“Sophie Fant”);
Costs of using virtual function • Each object has its size increased by the amount needed to hold an address • For each class, the compiler creates a table (an array) of addresses of virtural functions • For each function call, there’s an extra step of going to a table to look up an address
Rules of virtual methods • Begin a class method declaration with the keyword virtual in a base class • Constructor cannot be virtual • Destructor should be virtual unless a class isn’t to be used as a base class • Friends cannot be virtual functions because friends are not class members • Statics cannot be virtual • If a derived class fails to redefine a function, the class will use the base version of the function • If base class declaration is overloaded, one needs to redefine all the base class version in derived class
Inheritance is-a relationship Sometimes applying the is-a rule is not as simple as it might appear For example, A circle is a special case of an ellipse. Problems arise when one is tempting to derive a circle class from an ellipse class.
Abstract base classes (ABC) • Solution: One can abstract from the Ellipse and Circle classes what they have in common and place those features in an ABC • Program acctabc.h, acctabc.cpp, usebrass3.cpp
Inheritance & dynamic memory allocation • How does inheritance interact with dynamic memory allocation? • Derived class doesn’t use new • Derived class does use new
Derived class without DMA • It is not necessary to self-defined constructors, copy constructor, destructor, assignment by using new and delete
Derived class with DMA • It needs to self-defined constructors, copy constructor, destructor, assignment by using new and delete
An inheritance example with DMA and Friends • Program dma.h, dma.cpp, usedma.cpp
Class design • Member functions that the compiler generates for you • Default constructors • Copy constructors • Assignment operators • Destructors • Class method considerations
Class method considerations • Constructor • Constructors aren’t inherited) • Destructor • Should provide virtual destructor in base class • Conversion constructors
Passing by value or reference • Passing an object by value or passing by reference • Passing by value involves a temporary copy constructor and then destructor • Efficiency sake • Inheritance using virtual function to accept a base-class reference argument
Public inheritance considerations Or use conversion constructor // Implicit upcast // Explicit downcast // Save upcast // Unsafe downcast Is-a relationship consideration Constructors are not inherited Down casting is not safe Assignment operator
Class D: protected A Class DD: public D Class CC: public C private private private protected protected protected D ObjD DD ObjDD CC ObjCC public public public Public/private/protected members
Virtual method considerations In the Inadequate() function Brass version ViewAcct() was called Brace object constructed by Brass copy constructor Automatic upcasting allows the constructor argument to refer to a BrassPlus object Inappropriate code
Other methods considerations A base class destructor should be virtual Friend function is not actually a class member, it is not inherited