Wednesday, December 5, 2018
Home > OOP > Inheritance in C++

Inheritance in object-oriented programming is that a class or an object is based on another class or object. I.e. it is sharing same properties or characteristics from another class or object. It is the ability of a class to acquire the behavior from another class. Usually, a class inherits from one class but inheritance in c++ allows the classes to inherit from more than one classes.   The main idea behind inheritance is that user can create new classes based on the previously defined classes. You can create new classes are by inheriting same characteristics from previously defined classes and adding new functionality of their own.

Inheritance in object-oriented programming Using C++

In C++ a class can inherit from another class using one of the access specifiers. i.e. public, private, protected.


In public inheritance, a derived class is inheriting publicly from the base class. It refers that the public members of base class become public members of derived. Similarly, the protected members of the base class become protected members of the derived class. As we know that private members of a class are not directly accessible outside their own class, hence we cannot access them directly here as well. While you can access them by using public or protected member functions of their own class.


When a class inherits using protected specifier from the base class than both public and protected members of the base class become protected members of derived class.


Private inheritance is that a class is inheriting privately from base class both public and protected members of the base class become private members of derived class.

The syntax for Inheritance in C++

In c++  a class can inherit from another class using the following syntax.

class A {
          // this is constructor for class A.
class B :public A
          // this is constructor for class B.

In the above-written syntax class B is inheriting publically from class A.
Inheritance in object-oriented programming

Parent & Child Classes

A Class from which another class is inheriting is called parent class or base class or superclass. While the class which inherits from another class is called child class or derived class or subclass.

Inheritance in C++ Example

#include "stdafx.h"
#include "iostream"

using namespace std;

class person
          string name;
          string address;
               name = "";
               address = "";

class student :public person
     string regNo;
     float cgpa;

          void getData()
               cout << "Please Enter Name : " ;
               getline(cin, name);
               cout << "Please Enter Address : " ;
               getline(cin, address);
               cout << "Please Enter Registration Number : " ;
               getline(cin, regNo);
               cout << "Please Enter cgpa : ";
               cin >> cgpa;
          void show()
               cout << "\n***Student Record*** \n\n";
               cout << "Name : " <<name<<endl;
               cout << "Address : " << address << endl;
               cout << "Registration Number : " << regNo << endl;
               cout << "CGPA : " << cgpa << endl;

void main()
     student s;

Output: –
Inheritance in c++ example

3 thoughts on “Inheritance in C++

Leave a Reply

Your email address will not be published. Required fields are marked *

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