C++ Single Inheritance

If a single class is derived from one base class then it is called single inheritance. In C++ single inheritance base and derived class exhibit one to one relation.

C++ Single Inheritance Block Diagram

C++ single inheritance

As shown in the figure, in C++ single inheritance only one class can be derived from the base class. Based on the visibility mode used or access specifier used while deriving, the properties of the base class are derived. Access specifier can be private, protected or public.

Click here to learn in detail about access specifiers and their use in inheritance

C++ Programming Single Inheritance Syntax

class A   // base class
class B : acess_specifier A   // derived class
} ;

C++ Single Inheritance Example

// inheritance.cpp
#include <iostream> 
using namespace std; 
class base    //single base class
     int x;
   void getdata()
     cout << "Enter the value of x = "; cin >> x;
class derive : public base    //single derived class
    int y;
   void readdata()
     cout << "Enter the value of y = "; cin >> y;
   void product()
     cout << "Product = " << x * y;
 int main()
    derive a;     //object of derived class
    return 0;
 }         //end of program


Enter the value of x = 3
Enter the value of y = 4
Product = 12


In this program class derive is publicly derived from the base class base. So the class derive inherits all the protected and public members of base class base i.e the protected and the public members of base class are accessible from class derive.

However private members can’t be accessed, although, we haven’t used any private data members in the base class.

With the object of the derived class, we can call the functions of both derived and base class.