Sunday, September 13, 2020

Inheritance Concept In C++


Inheritance Concept in C++

Inheritance is one of the most important feature of Object Oriented Programming System(OOPs). A class to derive the properties and characteristics from another class is called Inheritance. That means the child class to acquire the properties (the data members and the member functions) of parent class.

Child Class:

A class that inherits properties from another class is known as child class, it is also known as derived class or subclass.

Parent Class:
         The class whose properties are inherited by other class is known as parent class, super class or base class.


The advantages of using inheritance in C++

The main advantages of inheritance are code reusability and readability.

When child class inherits the properties and functionality of parent class, we need not to write the same code again in child class.

This makes it easier to reuse the code and the code becomes much more readable

Let’s take a real life example to understand this:

Consider a group of vehicles. You need to create classes for Bus, Car and Truck.The methods fuelAmount(), capacity(), applyBrakes() will be same for all of the three classes. If we create these classes avoiding inheritance then we have to write all of these functions

Not used inheritance feature:

Applied inheritance feature:

Implementing inheritance in C++

Syntax :

class subclassName :  access_mode baseclassName

{

... .. ...

... .. ...

};

Here, subclass_name is the name of the sub class, 

access_mode is the mode in which you want to inherit this sub class for example: public, private etc.

base_class_name is the name of the base class from which you want to inherit the sub class

Note: A derived class doesn’t inherit access to private data members

Example Program:

#include <iostream>

using namespace std;   

class Parent

{   

   public:

      int id_p; }   

 

// Sub class inheriting from Base Class(Parent)

class Child : public Parent

{

    public:

      int id_c; };

int main()   

{   

    Child obj1;

     obj1.id_c = 7;

     obj1.id_p = 91;

     cout << "Child id is " <<  obj1.id_c << endl;

      cout << "Parent id is " <<  obj1.id_p << endl;

     return 0;   

}   

Output: 

 Child id is 7

 Parent id is 91

Modes of Inheritance (Access Specifiers)

Public mode: If we derive a sub class from a public base class. Then the public member of the base class will become public in the derived class and protected members of the base class will become protected in derived class.

 

Protected mode: If we derive a sub class from a Protected base class. Then both public member and protected members of the base class will become protected in derived class.

 

Private mode: If we derive a sub class from a Private base class. Then both public member and protected members of the base class will become Private in derived class.

 

Types of Inheritance in C++

1.     Single Inheritance

2.    Multiple Inheritance

3.    Multilevel Inheritance

4.    Hierarchical Inheritance

5.    Hybrid (Virtual) Inheritance

 

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.

EXAMPLE

#include <iostream>

using namespace std;

class Vehicle {    // base class

public:

          Vehicle()

          {

          cout << "This is a Vehicle" << endl;

          }

};

class Car: public Vehicle{

 

};

int main()

{

          Car obj;

          return 0;

}

Output:

This is a vehicle

 

Multiple Inheritance

 


Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes.

Example:

#include <iostream>

using namespace std;

class Vehicle {

  public:

    Vehicle()

    {

      cout << "This is a Vehicle" << endl;

    }

};

class FourWheeler {

  public:

    FourWheeler()

    {

      cout << "This is a 4 wheeler Vehicle" << endl;

    }

};

  class Car: public Vehicle, public FourWheeler {

 

};

int main()

{   

Car obj;

    return 0;

}

 

Output:

This is a Vehicle

This is a 4 wheeler Vehicle

 

Multilevel Inheritance

In this type of inheritance, a derived class is created from another derived class.

Here class B is derived class for Class C

And Class A is derived class for Class B. But Class A can access rights of Class B and Class C.

Example:

#include <iostream>

using namespace std;

class Vehicle 

{

  public:

    Vehicle()

    {

      cout << "This is a Vehicle" << endl;

    }

};

class fourWheeler: public Vehicle

{  public:

    fourWheeler()

    {

      cout<<"Objects with 4 wheels are vehicles"<<endl;

    }

};

class Car: public fourWheeler{

   public:

     car()

     {

       cout<<"Car has 4 Wheels"<<endl;

     }

};

int main()

{   

Car obj;     return 0; }

output:

This is a Vehicle

Objects with 4 wheels are vehicles

Car has 4 Wheels

 

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

Example:

#include <iostream>

using namespace std;

class Vehicle 

{

  public:

    Vehicle()

    {

      cout << "This is a Vehicle" << endl;

    }

};

class Car: public Vehicle

{

 

};

// second sub class

class Bus: public Vehicle

{

     

};

 

// main function

int main()

{   

    // creating object of sub class will

    // invoke the constructor of base class

    Car obj1;

    Bus obj2;

    return 0;

}

Output:

This is a Vehicle

This is a Vehicle

 

Hybrid (Virtual) Inheritance

              

Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance.

Above image shows the combination of hierarchical and multiple inheritance:

Example:

#include <iostream>

using namespace std;

class Vehicle 

{

  public:

    Vehicle()

    {

      cout << "This is a Vehicle" << endl;

    }

};

class Fare

{

    public:

    Fare()

    {

        cout<<"Fare of Vehicle\n";

    }

};

 class Car: public Vehicle

{   };

class Bus: public Vehicle, public Fare

{ };

int main()

{   

   Bus obj2;

    return 0;

}

 

 

Output:

This is a Vehicle

Fare of Vehicle

 


No comments:

Post a Comment