AppDividend
Latest Code Tutorials

Inheritance in C++ Example | C++ Inheritance Tutorial

0

Inheritance in C++ Example | C++ Inheritance Tutorial os today’s topic. When one class can access all the properties and characteristics of another class, it is known as inheritance. In more technical words, we can say that when an object of base class acquires all the properties of the parent object automatically and we can access the features of the parent class with the help of child’s object then it is an inheritance.

Inheritance in C++

Inheritance is a fundamental concept of object-oriented programming.

The class whose members are inherited are called base or parent class or superclass, and the class who inherits those properties are usually known as derived or child class or subclass.

#Advantages

#Reusability

By using inheritance, we can use the public methods without rewriting the code multiple types.

#Data hiding

The base class has the feature to keep data private, which cannot be altered by a derived class.

#Overriding

With inheritance, we have the feature to override the functions present in the base class to some more meaningful structure in the derived class.

#Extensibility

We can extend the base class logic depending on the business requirement of the derived class.

#Types of Inheritance in C++

There are 5 types of inheritance:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance

We are going to study these different types of inheritance in detail with examples. Before understanding it, let us understand the concept of the base class and derived class once again because we are going to use it as a reference for defining the different types of inheritance.

#Base Class

The base class is the parent class whose members and properties are generally inherited in the derived class.

#Derived Class

In simple words, the derived class is a class which is derived from a base class or the class in which the properties of the base class is inherited.

#Syntax of Derived class

class  derived_class ::  visibility_mode  base_class

See the following example.

class A:: public B   (Here A is the derived class which will be using         
                      the public feature of the parent class which is B)

Now let’s start with different types of inheritance.

#Single Inheritance

It is an inheritance in which the derived class is inherited from at most single base class. (Examples are added at the end of the article)

 

Single Inheritance

#Multilevel Inheritance

When one class inherits the properties of another class, which is further inherited by another class, it is known as multilevel inheritance.

 

Multilevel Inheritance

#Multiple Inheritance

When a subclass or derived class is derived from more than one base class, it is known as multiple inheritances.

 

Multiple Inheritance

#Hybrid Inheritance

It is the combination of more than one type of inheritance is known as hybrid inheritance.

 

Hybrid Inheritance

#Hierarchical Inheritance

It is an inheritance in which we usually derive more than one derived class from a particular base class.

 

Hierarchical Inheritance

Graphical representation of all types of inheritance.

 

Inheritance in C++ Example

#Modes of inheritance

#Public mode

If we derive a child class from a public parent class. Then the public member of the parent class becomes a public member for the child class and protected members of parent class becomes protected members of the child class.

#Protected mode

If we derive child class from a protected base class, then the public, as well as a protected member of the parent class, becomes the protected members of the child class.

#Private mode

If we derive a child class from a private base class, then the public, as well as protected members, become private for the derived class.

Private members of a base class cannot be directly accessed in the derived class in any circumstance.

#Table for summarizing the modes of inheritance

Base class access specifier                                 Inheritance Type
Public Protected Private
Public Public Protected Private
Protected Protected Protected Private
Private Private Private   Private

 

#Examples Programs for Inheritance in C++

#Example: 1

Write a simple program to show single inheritance.

#include <iostream>
using namespace std;

// base class
class Test
{
public:
  void Test_function()
  {
    cout << "I am a testing function" << endl;
  }
};

class Test_2 : public Test
{
};

int main()
{

  Test_2 obj;
  obj.Test_function(); //invoking the function of base class(single inheritance)
  return 0;
}

See the following output.

Examples Programs

 

#Example: 2

Write a program to show multiple inheritance.

#include <iostream>
using namespace std;

class patna
{ //Creating base class of patna
public:
  patna()
  {
    cout << "Hello I am from base class of Patna !! " << endl;
  }
};

class delhi
{
public:
  delhi()
  {
    cout << "Hello I am from base class of Delhi" << endl; //creating base class of delhi
  }
};

class check : public patna, public delhi //derived class of two base classes
{
public:
  void check_fun()
  {
    cout << "hello I am derived class function" << endl;
  }
};

int main()
{
  check obj;
  obj.check_fun();
  return 0;
}

See the following output.

 

output

#Example: 3

Write a program to show multilevel inheritance.

#include <iostream>
using namespace std;

class city
{
public:
  city()
  {
    cout << "Hello I am the 1st base class of cities" << endl;
  }
};
class delhi : public city
{
public:
  delhi()
  {
    cout << "Hello I am the capital of India Delhi" << endl;
  }
};
class location : public delhi
{
public:
  location()
  {
    cout << "I am vasant kunj(location) in Delhi" << endl;
  }
};

// main function
int main()
{
  location obj;
  return 0;
}

See the following output.

 

C++ Inheritance Tutorial

#Example: 4

Write a program to show hierarchical inheritance.

#include <iostream>
using namespace std;

class city
{
public:
  city()
  {
    cout << "I am the base class of cities" << endl;
  }
};

class delhi : public city
{
public:
  void check_1()
  {
    cout << "I am the capital of India Delhi" << endl;
  }
};

class mumbai : public city
{
public:
  void check_2()
  {
    cout << "I am the city of dreams Mumbai" << endl;
  }
};

int main()
{
  delhi obj1;
  mumbai obj2; //constructor will be called two times as both the objects will call the constructor of the base class.
  obj1.check_1();
  obj2.check_2();

  return 0;
}

See the following output.

 

Example 4

#Example: 5

Write a program to show hybrid inheritance.

#include <iostream>
using namespace std;

class City // first base class
{
public:
  City()
  {
    cout << "I am the base class of cities." << endl;
  }
};

class countries // second base class
{
public:
  countries()
  {
    cout << "I am the base class countries\n"
         << endl;
  }
};

// first sub class
class delhi : public City //single inheritance
{
public:
  void check_1()
  {
    cout << "I am the capital of India - Delhi " << endl;
  }
};

class mumbai : public City, public countries //Multiple inheritance
{
public:
  void check_2()
  {
    cout << "\nI am the city of Mumbai in India  " << endl;
  }
};

int main()
{
  mumbai obj1;
  delhi obj2; //constructors will run according to the object created
  obj1.check_2();
  obj2.check_1();
  return 0;
}

See the following output.

 

C++ Inheritance

Finally, Inheritance in C++ Example | C++ Inheritance Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.