What is Single Inheritance in C++

- March 03, 2018
When a derived class derives properties from one base class, it is referred to as Single Inheritance. The derived class inherit all data members and member functions of base class. It can also have its own members.
Singleton inheritance character

In the above image, a new class B is derived from the base class A. The derived class has its own two members.
Where subclass inherit the features of superclass. A class acquires the properties of another class.

Declaration

The general syntax to declare simple derived class:
class derived_class_name : visibility-mode (public, protected or private) base_class_name
{
  ..... //
  ..... // body of derived class
  ..... //
};

Constructors and Destructors

A part of the derived class is also included when an object of derived class is build. In fact, the base class is initiated to include the base class part with that object of derived class and then derived class part is included. The constructor of the base class is automatically executed when derived class object is created. The base class constructors are not inherited by derived class. However, a derived class constructor always calls the constructor for its base class first to initialize base class data members for the objects of derived class. The derived class default constructor calls if the derived class constructor is omitted.
Destructors are called in the reverse order of constructor calls. So a derived class destructor is called before its base class destructor.

Arguments

The declaration of the derived class is different, in case of constructor of base class with arguments. The base class constructor is connected with derived class constructor by using the colon (:) in the head of the derived class constructor. The parameters of the base class constructor are also given as parameter list in derived class constructor. The parameters of the base class come first in derived class constructor.

Single Inheritance Example Programs

//single inheritance example program
#include<iostream>
using namespace std;
  class A
   {
      private:
          int x, y;
      public:
          A (int m, int n)
          {
              x = m;
              y = n;
          }
      void print (void)
     {
       cout<<"Value of x = "<<x<<endl;
       cout<<"Value of y = "<<y<<endl;
      }
  };
 class B : public A
     {
      private:
             float j, k;
      public:
             B (int m, int n, float a, float b) : A (m, n)
  {
      j = a;
      k = b;
  }
   void show (void)
  {
        cout<<"Valur of j = "<<j<<endl;
        cout<<"Value of k = "<<k<<endl;
  }
};
main ()
{
   B obj (10, 8, 15.2, 4.3);
   obj .print();
   obj .show();
   getch();
 }

Output of Program

Value of a = 10
Value of b = 8
Value of j = 15.2
Value of k = 4.3

In the above C++ program, the object "obj" of derived class uses four values that passed to the constructor of derived class B which uses four parameters. The first two parameters "m" and "n" are used for base class construw. The arguments "a" and "b" are used for derived class constructor.

Example 2: Single inheritance using public visibility-mode.

#include<iostream>
using namespace std;
class customer
{
  char name [15];
  int age;
  public :
    void get()
    {
      cout << "Enter Customer Name :"<<endl;
      cin >> name;
      cout << "\nEnter Customer Age :"<<endl;
      cin >> age;
    }
    void show()
    {
      cout << "\nYour name is "<<name <<"\n and your age is "<<age;
    }
};
class manager : public customer
{
  int now;
  public :
    void get()
    {
      customer::get();
     
      cout << "\nEnter the number of customer under you : \n";
      cin >> now;
    }
    void show()
    {
      customer::show();
     
      cout << "\nNumber of customers under you is " << now;
    }
};
int main ()
{
manager m1;
m1.get();
m1.show();
return 0;
}