Inheritance Program in C++
What is Inheritance?
Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance.
The ability or process of inheritance allows using properties of one class to other class. The class whose properties are utilized is referred to as the "base class" or "parent class," and the class that uses them is known as the "derived class" or "child class." The derived class is now referred to as having inherited properties from the base class.
When we say that a derived class inherits the base class, we mean that it does so while maintaining all of the base class's properties and having the option to add new features of its own. There will be no impact on the base class from these new features in the derived class. The specialized class for the base class is the derived class.
Subclass or Derived Class: A class that receives properties from another class is referred to as a subclass or derived class.
Super Class: A subclass's base class or superclass is the class from which its properties are inherited.
Inheritance Programs example in C++
Simple Inheritance Programs
1. Program:
#include <iostream>
using namespace std;
class Fine
{
public: void display()
{
cout<<"This display is located inside the first-class section." << endl;
}
};
class good: public Fine
{
public: void show()
{
cout<<"This programme belongs to the second class, which is derived from the parent class." << endl;
}
};
int main()
{
Fine x;
x.display();
//x.show();
good y;
y.display();
y.show();
}
Explanation:
- In above code, we created two classes named Fine and Good. The good class is what we deduced from the fine.
- We have two functions: one in the base class and one in the derived class.
- We have declared our objects in the main method for both the parent class and the child class.
- We attempted to access both the base and derived class methods with the child class object, and both attempts were completely successful.
- However, as seen in the second result, if we attempt to access a variable or method from a derived class through an object from the base class, we encounter an error.
- It is clear because derived class methods and variables cannot be accessed by base class objects but vice versa.
Program Output:

Multiple inheritance example.
2. Program:
#include <iostream>
using namespace std;
// base class 1
class data_science
{
public:
void data_science_Marks()
{
cout << "The score for data science is 93."<<endl;
}
};
// base class 2
class Programming
{
public:
void Programming_Marks()
{
cout << "The score for Programming is 87."<<endl;
}
};
// derived class
class output: public data_science, public Programming
{
public:
void show_Marks()
{
// accessing the member functions
// of the base classes
data_science_Marks();
Programming_Marks();
}
};
int main()
{
// create an object of the derived class
output result;
// call displayMarks() function of the derived class
result.show_Marks();
return 0;
}
Explanation:
- First, we created 1st Base class named data_science.
- 1st Base class consists of a public function, "data_science_Marks," which has void data type.
- After that, we created the 2nd Base class, which name is Programming.
- 2nd Base class consists of a public function, "Programming_Marks," which has void data type.
- Then we created a Derived class whose name is "output," and this class inherits "data_science” and “Programming” classes in Public mode.
- This "output" class consists of the Public member function “show_Marks," which has a void data type.
- The member functions "data_science_Marks" and "Programming_Marks" of the base classes "data_science" and "Programming," respectively, are accessed by the function "show Marks."
- In the main() function, the Object "result" is created of the "output" Class.
- The function “show_Marks" is called by the object "result".
Program Output:

Multilevel Inheritance example.
3. Program:
#include <iostream>
using namespace std;
class Animal
{
public:
Animal ()
{
cout << "This is an Animal" << endl;
}
};
class Reptiles: public Animal
{
public:
Reptiles()
{
cout<<"Reptiles are a class of vertebrates"<<endl;
}
};
class Snake: public Reptiles{
public:
Snake()
{
cout<<"In reptiles, snakes are common."<<endl;
}
};
int main ()
{
Snake obj;
return 0;
}
Explanation:
- First, we created a Base class whose name is Animal.
- The base class consists of a Public function Animal(). This function will print a message on the screen "This is an animal".
- Then we created a derived class named Reptiles, and this class inherits the "Animal” class in public mode.
- This “Reptiles" class consists of the public member function “Reptiles”. This function will print a message on the screen "Reptiles are a class of vertebrates”.
- Then we created 2nd derived class, which name is Snake, and this class inherits the "Reptiles" class in public mode.
- This “Snake" class consists of the public member function "Snake”. This function will print a message on the screen "In reptiles, snakes are common.”.
Program Output:

Multiple inheritance example to get the sum of numbers.
4. Program:
#include<iostream>
using namespace std;
class A // 1st base class
{
protected:
int Aint;
public:
void set_Aint(int x)
{
Aint = x;
}
};
class B // 2nd base class
{
protected:
int Bint;
public:
void set_Bint(int x)
{
Bint = x;
}
};
class C // 3rd base class
{
protected:
int Cint;
public:
void set_Cint(int x)
{
Cint = x;
}
};
class Derived : public A, public B, public C
{
public:
void show(){
cout << "The value of 1st Base is " << Aint<<endl;
cout << "The value of 2nd Base is " << Bint<<endl;
cout << "The value of 3rd Base is " << Cint<<endl;
cout << "The sum of these values is " << Aint + Bint + Cint << endl;
}
};
int main()
{
Derived david;
david.set_Aint(55);
david.set_Bint(50);
david.set_Cint(75);
david.show();
return 0;
}
Explanation:
- First, we developed a class called "A" that contains the protected data member integer "Aint".
- Afterward, a public function called "set Aint" is part of the "A" class. The value of the data member "Aint" will be set by this function.
- Then, a "B" class was made, which is composed of the protected data member integer "Bint".
- "set Bint" is a public function that is part of the "B" class. The "Bint" data member will have its value set by this function.
- A protected data member integer, "Cint," was then established as part of the "C" class.
- Afterward, a public function called "set Cint" is part of the "C" class. The "Cint" data member will have its value set by this function.
- Then, in public mode, we built a class called "Derived" that inherits from classes "A," "B," and "C."
- Then the "Derived" class consists of the public member function "show".
- Then "show" will first print the values of "Aint," "Bint," and "Cint" separately before printing the sum of all three values.
- It is obvious that class "Derived" inherits from classes "A," "B," and "C." This is an illustration of multiple inheritances.
- The "Derived" data type object "david" is then constructed.
- Then the object "david" calls the function "set Aint" and passes the value "55" to it.
- The object "david" then calls the function "set Bint," passing the value "50" along with it.
- After that, the object "david" calls the function "set Cint" and passes the parameter "75" to it.
- The object "David" then makes a call to the function "show."
Program Output:

C++ Program of Hierarchal inheritance.
5. Program:
#include <iostream>
using namespace std;
// base class
class measurement
{
public:
int length = 9;
int breadth = 8;
};
// derived class 1
class circumference: public measurement
{
public:
// print_circumference() function of the derived class 1
// to print the circumference
void print_circumference()
{
// calculating the perimeter using the
// dimensions in the base class.
cout << "The calculated circumference is: " << 2 * (length + breadth) << endl;
}
};
// derived class 2
class area: public measurement
{
public:
// print_area() function of the derived class 2
// to print the circumference
void print_area()
{
// calculating the area using the
// measurement in the base class.
cout << "The calculated area is: " << (length * breadth) << endl;
}
};
int main()
{
// object of the derived class 1
circumference output1;
// object of the derived class 2
area output2;
// call print_circumference() function of the derived class 1
output1.print_circumference();
// call print_area() function of the derived class 2
output2.print_area();
return 0;
}
Explanation:
- First, we created 1st Base class, with name measurement.
- 1st Base class consists of public data members with integers "length" and "breadth".
- Then, we created 1st derived class with name "circumference," which inherits the "measurement" class in Public mode.
- This "circumference" class consists of the Public member function "print_circumference()," which has a void data type.
- This function will print the calculated circumference of the data member “length” and “breadth”.
- Then, we created 2nd derived class, with the name "area," and this class inherits the "measurement" class in Public mode.
- This "area" class consists of the Public member function "print_area()," which has a void data type.
- This function will print the calculated area of the data member “length” and “breadth”.
- In the main() function, Object “output1” of the derived class 1st is created, which has a "circumference" data type.
- Then Object “output2” of the derived class 2nd is created, which has the "area" data type.
- The function "print_circumference()" is called by the object "output1".
- The function "print_area()" is called by the object "output2".
Program Output:

C++ program of Hybrid inheritance.
6. Program:
#include <iostream>
using namespace std;
class Books // indicates class A
{
public:
Books()
{
cout<< "This is a Book"<<endl;
}
};
class Programming: public Books // indicates class B derived from class A
{
public:
Programming()
{
cout<< "This is a Programming Book"<<endl;
}
};
class Data_science // indicates class C
{
public:
Data_science()
{
cout<< "This is a Data Science Book"<<endl;
}
};
class oops: public Programming, public Data_science
// indicates class D derived from class B and class C
{
public:
oops()
{
cout<< "oops is the part of programming"<<endl;
}
};
int main() {
oops x;
return 0;
}
Explanation:
- First, we created 1st class, which name is Books.
- 1st class consists of a public function, "Books". This function will print the message of the book.
- Then, we created 2nd class, which name is Programming, and this class inherits the "Books" class in Public mode. This indicates that the class "Programming" is derived from the class "Books".
- Class "Programming" consist of a Public function, "Programming()," and this function will print a message, "This is a Programming Book".
- Then, we created 3rd class whose name is Data_science.
- 3rd class consists of a public function, "Data_science()". This function will print the message "This is a Data Science Book".
- Then we created a 4th class whose name is oops, and this class inherits the "Programming" and "Data_science" classes in Public mode. This indicates that class “oops” is derived from class “Programming” and “Data_science”.
- Class "oops" consist of a Public function "oops()," and this function will print a message "oops is the part of programming".
Program Output:

C++ Program to demonstrate the Ambiguity Resolution in Inheritance.
7. Program:
#include<iostream>
using namespace std;
class A // 1st Base class
{
public:
void saket(){
cout<<"You are looking good"<<endl;
}
};
class B // 2nd Base class
{
public:
void saket()
{
cout << "You are intelligent" << endl;
}
};
class Derived : public A, public B
{
int x;
public:
void saket(){
B :: saket();
}
};
int main()
{
// Ambibuity
A Aobj;
B Bobj;
Aobj.saket();
Bobj.saket();
Derived y;
y.saket();
return 0;
}
Explanation:
- We have created a “A” class which consists of public member function “saket”. The function “saket” will print “You are looking good”
- We have created a “B” class which consists of public member function “saket”. The function “saket” will print “You are intelligent”
- We have created a "Derived" class that inherits "A" and "B" classes. The "Derived" class consists of public member function "saket”.
- As a result of the usage of a scope resolution operator to inform the compiler which function to execute in the absence of ambiguity, the function "saket" will execute the "saket" function of the "B" class.
- The "A" data type object "Aobj" is created.
- The "B" data type object "Bobj" is created.
- The object "Aobj" calls the function "saket".
- The object "Bobj" calls the function "saket".
- The "Derived" data type is used to generate the object "y."
- The object "y" calls the function "saket."
- The most important thing to take away from this is that the "saket" function of the "B" class will be invoked when the object "y" calls the function "saket," as we stated it using the scope resolution operator "::" to eliminate ambiguity.
Program Output:
