1. 1
Chapter 8 - Inheritance
Outline
8.1 Introduction
8.2 Defining Derived Classes
8.3 Single Inheritance
8.4 Making Private Member Inhritable
8.5 Multiple Inheritance
8.6 Multiple Inheritance
8.7 Hierarchical Inheritance
8.8 Hybrid Inheritance
8.9 Virtual Base Classes
8.10 Abstract Classes
8.11 Constructors and Destructors in Derived Classes
2. 2
8.1 Introduction
• Inheritance
– Software reusability
– The mechanism of deriving a new class from old one is called
inheritance
– OLD CLASS is called as BASE CLASS
– NEW CLASS is called as DERIVED CLASS or Subclass .
– Create new class from existing class
• Absorb existing class’s data and behaviors
• Enhance with new capabilities
– Derived class inherits from base class
• Derived class
– More specialized group of objects
– Behaviors inherited from base class
• Can customize
– Additional behaviors
3. 3
8.1 Introduction
• Class hierarchy
– Direct base class
• Inherited explicitly (one level up hierarchy)
– Indirect base class
• Inherited two or more levels up hierarchy
– Single inheritance
• Inherits from one base class
– Multiple inheritance
• Inherits from multiple base classes
– Base classes possibly unrelated
• Chapter 22
5. 5
8.1 Defining Derived Class
• Three types of inheritance
– public
• Every object of derived class also object of base class
– Base-class objects not objects of derived classes
– Example: All cars vehicles, but not all vehicles cars
• Can access non-private members of base class
– Derived class can effect change to private base-class
members
• Through inherited non-private member functions
– private
• Alternative to composition
– Protected
6. 6
“Protected” Access
• We have seen two access modes in C++ classes:
public and private
– Public members are directly accessible by users of the
class
– Private members are NOT directly accessible by users of
the class, not even by inheritors
• There is a 3rd
access mode: protected
– Protected members are directly accessible by derived
classes but not by other users
7. 7
8.1 Defining Derived Class
• Abstraction
– Focus on commonalities among objects in system
• “is-a” vs. “has-a”
– “is-a”
• Inheritance
• Derived class object treated as base class object
• Example: Car is a vehicle
– Vehicle properties/behaviors also car properties/behaviors
– “has-a”
• Composition
• Object contains one or more objects of other classes as
members
• Example: Car has a steering wheel
8. 8
8.2 Defining Derived Classes
• Base classes and derived classes
– Object of one class “is an” object of another class
• Example: Rectangle is quadrilateral.
– Class Rectangle inherits from class Quadrilateral
– Quadrilateral: base class
– Rectangle: derived class
– Base class typically represents larger set of objects than
derived classes
• Example:
– Base class: Vehicle
• Cars, trucks, boats, bicycles, …
– Derived class: Car
• Smaller, more-specific subset of vehicles
9. 9
Defining Derived Classes
• Inheritance examples
Base class Derived classes
Student GraduateStudent
UndergraduateStudent
Shape Circle
Triangle
Rectangle
Loan CarLoan
HomeImprovementLoan
MortgageLoan
Employee FacultyMember
StaffMember
Account CheckingAccount
SavingsAccount
10. 10
8.2 Defining Derived Classes
• Inheritance hierarchy
– Inheritance relationships: tree-like hierarchy structure
– Each class becomes
• Base class
– Supply data/behaviors to other classes
OR
• Derived class
– Inherit data/behaviors from other classes
17. 17
8.4 Type of Inheritance:
• Public Inheritance: When deriving a class from a public
base class, public members of the base class become
public members of the derived class
• protected members of the base class become protected
members of the derived class.
• A base class's private members are never accessible
directly from a derived class, but can be accessed through
calls to the publicand protected members of the base
class.
18. • Protected Inheritance: When deriving from a
protected base class, public and protected
members of the base class become protected
members of the derived class.
• Private Inheritance: When deriving from a
private base class, public and protected members
of the base class become private members of the
derived class
18
22. 22
8.5 Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
class top //base class
{
public :
int a;
void getdata()
{
cout<<"nnEnter first Number :::t";
cin>>a;
}
void putdata()
{
cout<<"nFirst Number Is :::t"<<a;
}
};
//First level inheritance
class middle :public top // class middle is derived_1
{
public:
int b;
void square()
{
getdata();
b=a*a;
cout<<"nnSquare Is :::"<<b;
}
};
//Second level inheritance
class bottom :public middle // class bottom is derived_2
{
public:
int c;
void cube()
{
square();
c=b*a;
cout<<"nnCube :::t"<<c;
}
};
int main()
28. 28
8.10 Abstract Class
• Base Class to be inherited by other classes
• No objects of base class
• Example:
29. 29
8.11 Constructors in Derived Classes
• Instantiating derived-class object
– Chain of constructor calls
• Derived-class constructor invokes base class constructor
– Implicitly or explicitly
• Base of inheritance hierarchy
– Last constructor called in chain
– First constructor body to finish executing
• Initializing data members
– Each base-class constructor initializes data members
• Inherited by derived class
31. 31
8.11 Constructors and Destructors in
Derived Classes
• Destroying derived-class object
– Chain of destructor calls
• Reverse order of constructor chain
• Destructor of derived-class called first
• Destructor of next base class up hierarchy next
– Continue up hierarchy until final base reached
• After final base-class destructor, object removed from
memory
32. 32
8.11 Destructors in Derived Classes
• Base-class constructors, destructors, assignment
operators
– Not inherited by derived classes
– Derived class constructors, assignment operators can call
• Constructors
• Assignment operators