Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is Single Inheritance in C++

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.


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.


Example Programs


//single inheritance example program

#include

#include

  class A

   {

      private:

          int x, y;

      public:

          A (int m, int n)

          {

              x = m;

              y = n;

          }

      void print (void)

     {

        cout
        cout
      }

  };

 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
        cout
  }

};

main ()

{

   clrscr();

   B obj (10, 8, 15.2, 4.3);

   obj .print();

   obj .show();

   getch();

 }



Output:
Value of a = 10
Value of b = 8
Value of j = 15.200001
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

#include

class customer

{

  char name [15];

  clrscr();

  int age;

  public :

    void get()

    {

      cout
      cin >> name;

      cout
      cin >> age;

    }

    void show()

    {

      cout     }

};

class manager : public customer

{

  int now;

  public :

    void get()

    {

      customer::get();

     

      cout
of customer under you : \n";

      cin >> now;

    }

    void show()

    {

      customer::show();

     

      cout
    }

};

int main ()

{

manager m1;

m1.get();

m1.show();

return 0;

}



This post first appeared on Programming Explain, please read the originial post: here

Share the post

What is Single Inheritance in C++

×

Subscribe to Programming Explain

Get updates delivered right to your inbox!

Thank you for your subscription

×