CPP Program to Implement the Single Inheritance Concept


To write a C++ program for implementing the Single inheritance concept.

Inheritance in C++

  • Inheritance is the capability of one class to acquire properties and characteristics from another class.
  • The class whose properties are inherited by another class is called the Parent or Base or Superclass.
  • And, the class which inherits properties of other class is called Child or Derived or Sub class.
  • Inheritance makes the code reusable.
  • When we inherit an existing class, all its methods and fields become available in the new class, hence code is reused.

NOTE: All members of a class except Private, are inherited

Purpose of Inheritance in C++

  1. Code Reusability
  2. Method Overriding (Hence, Runtime Polymorphism.)
  3. Use of Virtual Keyword

Access Control and Inheritance

  • A derived class can access all the non-private members of its base class.
  • Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to – who can access them in the following way −

Same classyesyesyes
Derived classesyesyesno
Outside classesyesnono

A derived class inherits all base class methods with the following exceptions −

  • Constructors, destructors and copy constructors of the base class.
  • Overloaded operators of the base class.
  • The friend functions of the base class.


Basic Syntax of Inheritance:

class Subclass_name : access_mode Superclass_name
  • While defining a subclass like this, the super class must be already defined or atleast declared before the subclass declaration.
  • Access Mode is used to specify, the mode in which the properties of superclass will be inherited into subclass, public, privtate or protected.

Access Modifiers and Inheritance: Visibility of Class Members

  • Depending on the Access modifier used while inheritance, the availability of class members of Superclass in the subclass changes.
  • It can either be private, protected, or public.

1) Public Inheritance

  • This is the most used inheritance mode.
  • In this the protected member of super class becomes protected members of sub class and public becomes public.
class Subclass : public Superclass

2) Private Inheritance

  • In private mode, the protected and public members of super class become private members of derived class.
class Subclass : Superclass   // By default its private inheritance

3) Protected Inheritance

  • In protected mode, the public and protected members of Super class becomes protected members of Sub class.
class subclass : protected Superclass

Types of Inheritance in C++

In C++, we have 5 different types of Inheritance. Namely,

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance (also known as Virtual Inheritance)

Single Inheritance in C++

  • In this type of inheritance one derived class inherits from only one base class.
  • It is the simplest form of Inheritance.


  1. Start the process.
  2. Define the base class with variables and functions.
  3. Define the derived class with variables and functions.
  4. Get two values in the main function.
  5. Define the object for derived class in the main function.
  6. Access member of the derived class and base class using the derived class object.
  7. Stop the process.


#include <iostream> 
using namespace std; 
class base    
int x;
void getdata()
cout << "Enter the value of x = "; cin >> x;cout<<x;
class derive : public base    
int y;
void readdata()
cout << "\nEnter the value of y = "; cin >> y;cout<<y;
void product()
cout << "\nProduct = " << x * y;
int main()
derive a;     
return 0;


17 3

Enter the value of x = 17
Enter the value of y = 3
Product = 51


Thus, the Implement the Single Inheritance Concept was executed successfully.

Leave a Comment