Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Inheritance in C++

Need for Inheritance


• Capability to express Inheritance relationship
• Reusability
• Transitive nature
• Derived class/Sub class
• Base class/ Super class
• Subclass is derived from Base class or inherits
from Base class
• To define a derived class, we use a class
derivation list to specify the base class(es).
• A class derivation list names one or more base
classes and has the form −
class derived-class: access-specifier base-class
Different forms of Inheritance
1. Single Inheritance :
In single inheritance, a class is allowed to
inherit from only one class. i.e. one sub class
is inherited by one base class only.
2. Multiple Inheritance:
where a class can inherit from more than one
classes. i.e one sub class is inherited from
more than one base classes
• Multilevel Inheritance:

In this type of inheritance, a derived class is


created from another derived class.
• Hierarchical Inheritance:

In this type of inheritance, more than one sub


class is inherited from a single base class. i.e.
more than one derived class is created from a
single base class.
• Hybrid (Virtual) Inheritance:
Hybrid Inheritance is implemented by
combining more than one type of inheritance.
For example: Combining Hierarchical
inheritance and Multiple Inheritance.
Inheritance program examples
// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

// sub class derived from two base classes


class Car: public Vehicle{

};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

// second base class


class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle" << endl;
}
};

// sub class derived from two base classes


class Car: public Vehicle, public FourWheeler {

};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class fourWheeler: public Vehicle
{ public:
fourWheeler()
{
cout<<"Objects with 4 wheels are vehicles"<<endl;
}
};
// sub class derived from two base classes
class Car: public fourWheeler{
public:
car()
{
cout<<"Car has 4 Wheels"<<endl;
}
};

// main function
int main()
{
//creating object of sub class will
//invoke the constructor of base classes
Car obj;
return 0;
}
Visibilty modes
public
• Derived class can access the public and
protected members of the base class but not
the private members
• this type does not change the access specifiers
of the inherited members of derived class
Private
• Derived class can access public and protected
members of the base class privately
• public and protected members of the base
class become private member to derived class
• This derived class cannot be inherited further
protected
• Derived class can access public and protected
members of the base class as protected
• This derived class is now not available to
outside world and can be accessed only
through member functions of derived and
classes that inherit this derived class
Access Specifier Accessible from Accessible from Accessible from
own class derived class/ objects outside the
Inheritable class
Public Yes Yes Yes
Protected Yes Yes No
Private yes No No
Significance of Visibility modes

You might also like