Inheritance Concept of Inheritance (What, Why, and How) Simple Example of Inheritance Base Classes and Derived Classes Private Member Data vs. Protected Member Data Base Class Access Specifications The Base Class Constructor Calls Overriding Member Functions
Inheritance (continue) Types of Inheritance More Examples of Simple Inheritance Examples of Multi-Level Inheritance Examples of Multiple Inheritance Homework and Lab Assignment
Concept of Inheritance What is inheritance? - to inherit properties/capabilities (member data and member functions) from an existing class or existing classes. - the existing class is called as a base class - the inherited class is called as a derived class - The two classes in the inheritance must have relationship in terms of code-reusable
Concept of Inheritance (continue) Why Inheritance? - code reusability (save time and money) - Increasing program reliability - Better problem-solving and program design - Supporting polymorphism
Concept of Inheritance (continue) Conceptual examples - example 1: circle base class: circle area = *r*r derived class: sphere sphere area = 4 *circle::area volume = 4/3*circle::area*r Sphere is kind of circular shape r r
Concept of Inheritance (continue) - example 2: base class: employee members: ID,name, salary employee() show() derived class: manager members: manager is kind of employee office, bonus manager() computeBonus() show()
Simple Examples of Inheritance Example of inheriting a circle class to a sphere class - \CS116\YgaoHandouts\Ch9\circlein.cpp Example of inheritance - (p ) Textbook example
Base Classes and Derived Classes Characteristics of a base class - has all features of the objects in the category - is a general description of all objects in the category in terms of member data and member functions Characteristics of a derived class - has detailed and specific features of the objects in the class - usually has more capabilities than the base class in terms of member data and member functions Examples: - circlesphereemployeemanager
Private Member Data vs. Protected Member Data Define the member data as protected in a base class if the derived class can directly access the data as it own member data Define the member data as private in a base class if the derived class can only access the data via the member functions defined in the base class. Example: CS116/Ygao/Handouts /Ch9/circlein.cpp
Base Class Access Specifications
Base Class Access Specifications (continues)
The Base Class Constructor Calls Explicit base constructor calls i.e., shpere(double radius) : circle(radius) { } The base class’s constructor is called before the derived class’s constructor. The destructors are called in reverse order. i.e., (p ) – textbook example
Overriding Member Functions The functions in a base class can be overridden in the derived classes Use the scope resolution operator :: to indicate the member functions called are from the base class. i.e., direct member function call area = 2*circle::Area() + Area(); the base member function call Must be used internally in the member functions of the derived class (p ) – textbook examples
Types of Inheritance Basically, there are three types of inheritance - simple inheritance: a base class derives on or many derived classes - multiple-level inheritance: a derived class can be served as a base class for next level of inheritance - multiple inheritance: there are more than one base class to derive one or many derived classess.
Types of Inheritance (continue) Example of simple inheritance
Types of Inheritance (continue) Example of multiple-level inheritance
Types of Inheritance (continue) Example of multiple inheritance
More Examples of Simple Inheritance /CS116/YGao/Handouts/Ch9/inherivd.cpp
Example of Multiple-level Inheritance /CS116/YGao/Handouts/Ch9/multilevel.cpp (p ) – textbook example
Example of Multiple Inheritance (p ) textbook example