1 / 46

Class Inheritance

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.

gerryt
Download Presentation

Class Inheritance

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

  2. Inheriting classes • Base class • Derived class • Program tabtenn0.h, tabtenn0.cpp, usett0.cpp

  3. Inheritance

  4. Derived class class RatedPlayer : public TableTennisPlayer { private: int rating; public: … };

  5. Base and derived classes

  6. Access specify without inheritance

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

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

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

  10. Using a derived class • Program tabtenn1.h, tabtenn1.cpp, usett1.cpp

  11. Access specify with inheritance

  12. Special relationship between base and derived class • Derived class uses base class methods • Upward casting is allowed • Downward casting is not safe

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

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

  15. Is-a & has-a relationships

  16. Inheritance model • Public • An is-a relationship • Illegal relationships • Is-like-a, has-a, is-implemented-as, uses-a • Private • Protected

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

  18. Bindings • Static binding (early binding) during compiling • Dynamic binding (late binding) during execution

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

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

  21. Upcasting downcasting

  22. Virtual member functions and dynamic binding • Virtual function uses dynamic binding, whereas, non-virtual function employs static binding

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

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

  25. How virtual function work? • Virtual function table (vtbl) Scientist sophie(“Sophie Fant”);

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

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

  28. Return type is allowed to vary

  29. Redefinition hides methods

  30. Hidden methods

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

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

  33. Inheritance & dynamic memory allocation • How does inheritance interact with dynamic memory allocation? • Derived class doesn’t use new • Derived class does use new

  34. Derived class without DMA • It is not necessary to self-defined constructors, copy constructor, destructor, assignment by using new and delete

  35. Derived class with DMA • It needs to self-defined constructors, copy constructor, destructor, assignment by using new and delete

  36. An inheritance example with DMA and Friends • Program dma.h, dma.cpp, usedma.cpp

  37. Class design • Member functions that the compiler generates for you • Default constructors • Copy constructors • Assignment operators • Destructors • Class method considerations

  38. Class method considerations • Constructor • Constructors aren’t inherited) • Destructor • Should provide virtual destructor in base class • Conversion constructors

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

  40. Returning an object or returning a reference

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

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

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

  44. Other methods considerations A base class destructor should be virtual Friend function is not actually a class member, it is not inherited

  45. The base class methods in public inheritance

  46. Member functions in class

More Related