What All is Inherited from Parent Class in C++



In C++, when a class inherits from another class, it inherits all members from parent class, but their accessibility are based on access specifiers.

Key Points on What Inherits from Parent Class

Following are some points on derived class inherits from its parent:

  • The derived class can inherit data members, member functions of base class.
  • If the data members are public, they can be accessed by derived class, same class and outside the class.
  • If data members are protected, they can be accessed by derived and same class only, but outside the class, they can not be accessed.
  • If data members are private, only same class can access them.

To perform any program using Object Oriented Programming(OOPs) in C++ always remember the modes of inheritance:

Inheritance Mode Public Members Protected Members
Public Public Protected
Protected Protected Protected
Private Private Private

Example of Inheritance

In the example, the derived class inherits the base class and its data members. Derived class object d is created and used to call the data members of base and derived classes a and x. But it can not access the variables b and c of the base class because they are protected and private. If we try to access them, it will show errors.

#include <bits/stdc++.h>
using namespace std;

class Base {
   public: int a;
   protected: int b;
   private: int c;
};

class Derived : public Base {
   public: int x;
};

int main() {
   Derived d;
   d.a = 10;
   d.x = 20;
   cout << "Derived class data member vale : " << d.x << endl;
   cout << "Base class data member value : " << d.a << endl;
   return 0;
}

The above program produces the following result:

Derived class data member vale : 20
Base class data member value : 10

In the above program, you see how a child or derived class inherited the properties of a parent class. Now, we have to add the properties of a derived class that are not inherited from the parent class.

  • Constructors and destructor: It is not inherited because the base class constructor can be explicitly invoked.
  • Friend functions: The friend function cannot access to private and protected member of a class.
  • Operator overloading: It is not inherited because the base class overload operator available in the derieved class. They are modified by the derieved class.

Example of Non-Inheritance

Based on all the above statements, you can see how non-inheritance properties works.

#include <iostream>
using namespace std;

class Parent {
protected:
   int value;
public:
   // Constructor initialization
   Parent(int v) : value(v) { cout << "Parent constructor called\n"; }
    
   // Display the destructor message when the object is destroyed
   ~Parent() { cout << "Parent destructor called\n"; }

   void show() { cout << "Value: " << value << endl; }

   // Declaration of friend function
   friend void displayValue(Parent &p);
};

// Definition of friend function
void displayValue(Parent &p) {
   cout << "Friend function accessing value: " << p.value << endl;
}

class Child : public Parent {

public:
   // The child constructor explicitly calls parent constructor
   Child(int v) : Parent(v) { cout << "Child constructor called\n"; }
   
   // The destructor display the message when object id  destroyed 
   ~Child() { cout << "Child destructor called\n"; }

   // operator overloading to add two child objects
   Child operator+(Child &other) {
       return Child(this->value + other.value);
   }
};

int main() {

   // creating two child objects
   Child c1(10), c2(20); 
   c1.show(); 

   // friend function
   displayValue(c1); 
 
   // overloaded operator
   Child c3 = c1 + c2; 
   c3.show(); // Display result

   return 0;
}

The above program produces the following output:

Child destructor called
Parent destructor called
Child destructor called
Parent destructor called
Updated on: 2025-06-04T14:22:42+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements