C++ Language - Session 7: Inheritance - FPT University

ppt 28 trang hoanguyen 4570
Bạn đang xem 20 trang mẫu của tài liệu "C++ Language - Session 7: Inheritance - FPT University", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pptc_language_session_7_inheritance_fpt_university.ppt

Nội dung text: C++ Language - Session 7: Inheritance - FPT University

  1. Session 7 – Inheritance Derived Classes Derived Functions Session 7 - Inheritance 1/28
  2. Objectives • Derived Classes – Terminology – Derivation – Protected Access • Derived Functions – Shadowing – Constructors – Destructors Session 7 - Inheritance 2/28
  3. Inheritance • An inheritance is a relationship where objects share a common structure: the structure of one object is a sub-structure of another object. Session 7 - Inheritance 3/28
  4. Inheritance • In object-oriented programming, we describe inheritance in terms of classes. Inherited classes share a common structure. We say that one class is a kind of another class. • For example, the Mammal class is a kind of Animal class. Session 7 - Inheritance 4/28
  5. Terminology(1) • The derived class inherits the structure of a base class. • For example, a mammal inherits the structure of an animal. Animal is the base class, while mammal is the derived class. Session 7 - Inheritance 5/28
  6. Terminology(2) • The subject of an "is-a-kind-of" relationship is the derived class. The object is the base class. Session 7 - Inheritance 6/28
  7. Derivation(1) • A derived class includes all of the instance variables and all of the normal member functions of the base class (A normal member function is a member function that is not a constructor, destructor or assignment operator). Session 7 - Inheritance 7/28
  8. Derivation(2) Syntax: class DerivedClassName : [accessModifier] BaseClass { }; • The access modifier defines the type of access that the derived class has to the non-private members of the base class. The most common modifier is public. Session 7 - Inheritance 8/28
  9. Derivation(2) Session 7 - Inheritance 9/28
  10. Protected Access • Access modifiers: – private - no access, – protected - access by derived classes only, and – public - unlimited access. Session 7 - Inheritance 10/28
  11. Access to Base Class Members • To grant to the functions of a derived class access to a data member or a member function of the base class, we qualify the member as either protected or public in the base class declaration. Session 7 - Inheritance 11/28
  12. Avoid Granting Protected Access to Data Members • If a base class variable has protected access, derived class functions can circumvent the base class' validation process. Session 7 - Inheritance 12/28
  13. Derived Functions • A derived function is a normal member function of a derived class. • A normal member function is a member function that is not a constructor, destructor or assignment operator. • A derived class inherits the normal member functions of its base class (or base classes). Session 7 - Inheritance 13/28
  14. Access Modifiers Demo Session 7 - Inheritance 14/28
  15. Shadowing • If a derived function has the same name as a base class function, the derived function shadows the member function of the base class. Session 7 - Inheritance 15/28
  16. Shadowing Demo Session 7 - Inheritance 16/28
  17. Accessing a Shadowed Function • Preface the function name with the name of the class within which that function is visible. • The call takes the form: ClassName::functionName( arguments ) Session 7 - Inheritance 17/28
  18. Hiding the Inheritance Hierarchy! • To avoid exposing the inheritance hierarchy, we call the base class display() function from within the derived display() function: Session 7 - Inheritance 18/28
  19. Constructors(1) • Constructors are not normal member functions. They are not inherited by a derived class. Each derived class has its own constructor. Session 7 - Inheritance 19/28
  20. Constructors(2) • The process of constructing an instance of a derived class involves several stages: – the base class portion is constructed first • memory is allocated for the instance variables in the order of their declaration, • the base class constructor code is executed, – the derived class portion is constructed afterwards • memory is allocated for the instance variables in the order of their declaration, • the derived class constructor is executed. Session 7 - Inheritance 20/28
  21. Constructors(3) Session 7 - Inheritance 21/28
  22. Constructors Demo Session 7 - Inheritance 22/28
  23. Constructors with Arguments • We can pass some or all of the values received by the constructor of a derived class to the constructor of the base class. DerivedClass (paras) : BaseClass (args) • There is no need to include the base class call if the call is to the no argument constructor. Session 7 - Inheritance 23/28
  24. Successive Initialization • The call from the constructor of a derived class to the constructor of the base class is always a call to the constructor of the immediate base class. • A call to the constructor of a base class that is higher in the inheritance hierarchy is invalid. Session 7 - Inheritance 24/28
  25. Destructors(1) • Destructors, like constructors, are not inherited. • Destructors execute in opposite order to the order of construction. The derived class destructor is called first and the base class destructor is called last. Session 7 - Inheritance 25/28
  26. Destructors(2) Session 7 - Inheritance 26/28
  27. Destructor Demo Session 7 - Inheritance 27/28
  28. Summary • Derived Classes – Terminology – Derivation – Protected Access • Derived Functions – Shadowing – Constructors – Destructors Q&A Session 7 - Inheritance 28/28