Table of Contents
ToggleWhat is Inheritance in C++?
Inheritance in C++ is a way to create a new class (called child class or derived class) from an existing class (called parent class or base class)
Inheritance in C++ involves creating a new class (called a child or derived class) based on an existing class (called a parent or base class), allowing the child class to utilize the properties and behaviors (variables and functions) of the parent class.
Base Class:
A base class is the original class that contains common features (variables and functions). Other classes can use it as a starting point. A base class is also known as a parent class
Derived Class:
A derived class is a new class that is created from the base class. It inherits the features of the base class and can also have its own extra features. A derived class is also known as a child class.
Access Control in C++
Access control determines the visibility and accessibility of class members (data members and member functions). C++ provides three access specifiers:
- public:
- Members are accessible from anywhere (inside the class, derived classes, and outside the class).
- Typically used for interface functions (e.g., getters, setters, or other methods meant for external use).
- protected:
- Members are accessible within the class and in derived classes.
- Not accessible from outside the class hierarchy (e.g., by objects or external functions).
- private:
- Members are accessible only within the class where they are defined.
- Not accessible in derived classes or outside the class, unless accessed via public/protected member functions.
We can summarize the different access types according to who can access them in the following way
| Access | public | protected | private |
|---|---|---|---|
| Same class | yes | yes | yes |
| Derived classes | yes | yes | no |
| Outside classes | yes | no | no |
Types of Inheritance / Form of Inheritance :
Single Inheritance: Single inheritance is a type of inheritance where one derived class inherits from only one base class. It allows the derived class to use the properties and functions of a single parent class. Declaration of single Inheritance isÂ
class A {
// Members of class A
};
class B : public A {
// Members of class B
}; 
Write a C++ program using single inheritance where:
- Class Father has a function that displays the father’s name.
- Class Son inherits from Father and has a function that displays the son’s name.
- Create an object of class Son and call both functions.
#include <iostream>
using namespace std;
// Base class
class Father {
public:
void showFatherName() {
cout << "Father's Name: Ram Sharma" << endl;
}
};
// Derived class
class Son : public Father {
public:
void showSonName() {
cout << "Son's Name: Sita Ram Sharma" << endl;
}
};
int main() {
Son obj;
obj.showFatherName(); // From base class Father
obj.showSonName(); // From derived class Son
return 0;
}
output of program
Father's Name: Ram Sharma
Son's Name: Sita Ram Sharma
Write a C++ program using single inheritance where:
- Class Teacher has a function that displays the teacher’s subject.
- Class Student inherits from Teacher and has a function that displays the student’s name.
- Create an object of class Student and call both functions.
#include <iostream>
using namespace std;
// Base class
class Teacher {
public:
void showSubject() {
cout << "Teacher's Subject: Science" << endl;
}
};
// Derived class
class Student : public Teacher {
public:
void showStudentInfo() {
cout << "Student's Name: Suman Sharma" << endl;
cout << "Student's Address: Kathmandu, Nepal" << endl;
}
};
int main() {
Student obj;
obj.showSubject(); // Function from Teacher class
obj.showStudentInfo(); // Function from Student class
return 0;
}
output of program
Teacher's Subject: Science
Student's Name: Suman Sharma
Student's Address: Kathmandu, Nepal
2. Multiple Inheritance:Â
In multiple inheritance, a single derived class can inherit from two or more base classes. This allows the derived class to use the properties and methods of all the base classes.
Multiple Inheritance means when one class gets the features (like variables and methods) from two or more classes. It’s like a student learning math from one teacher and science from another teacher — the student is learning from both.
Syntax of Multipale Inherantance
class A {
// code for class A
};
class B {
// code for class B
};
class C : public A, public B {
// class C inherits from both A and B
};

Write a program to create three classes, A, B, and C, where class C inherits from both A and B. Each class has a function that prints a message. Display all messages using an object of class C.

#include <iostream>
using namespace std;
class A {
public:
void displayA() {
cout << "This is class A" << endl;
}
};
class B {
public:
void displayB() {
cout << "This is class B" << endl;
}
};
class C : public A, public B {
public:
void displayC() {
cout << "This is class C" << endl;
}
};
int main() {
C obj;
obj.displayA();
obj.displayB();
obj.displayC();
return 0;
}
output:
This is class A
This is class B
This is class C
 Create a program with classes Student, Marks, and Result. The Result class should inherit from both Student and Marks, and display the total marks of a student.
#include <iostream>
using namespace std;
class Student {
public:
int roll;
void getRoll(int r) {
roll = r;
}
};
class Marks {
public:
int math, science;
void getMarks(int m, int s) {
math = m;
science = s;
}
};
class Result : public Student, public Marks {
public:
void displayResult() {
cout << "Roll No: " << roll << endl;
cout << "Total Marks: " << (math + science) << endl;
}
};
int main() {
Result r;
r.getRoll(101);
r.getMarks(80, 90);
r.displayResult();
return 0;
}Â Create a class Engine and Wheel that sets engine capacity and number of wheels without function arguments. Create a class vehicle that inherits from both and displays the vehicle details.
#include <iostream>
using namespace std;
class Engine {
public:
int capacity;
void setCapacity() {
capacity = 2000; // in cc
}
};
class Wheels {
public:
int number;
void setNumber() {
number = 4;
}
};
class Vehicle : public Engine, public Wheels {
public:
void showDetails() {
cout << "Engine capacity: " << capacity << " cc" << endl;
cout << "Number of wheels: " << number << endl;
}
};
int main() {
Vehicle v;
v.setCapacity();
v.setNumber();
v.showDetails();
return 0;
}
output:
Engine capacity: 2000 cc
Number of wheels: 4Â Create classes Theory and practical that set theory and practical marks without function arguments. Create a class Total Marks that inherits from both and displays the total marks.
#include <iostream>
using namespace std;
class Theory {
public:
int marks;
void setMarks() {
marks = 75;
}
};
class Practical {
public:
int marks;
void setMarks() {
marks = 25;
}
};
class TotalMarks : public Theory, public Practical {
public:
void displayTotal() {
cout << "Theory marks: " << Theory::marks << endl;
cout << "Practical marks: " << Practical::marks << endl;
cout << "Total marks: " << Theory::marks + Practical::marks << endl;
}
};
int main() {
TotalMarks tm;
tm.setMarks(); // sets marks for Theory
tm.Practical::setMarks(); // sets marks for Practical explicitly
tm.displayTotal();
return 0;
}
output:
Theory marks: 75
Practical marks: 25
Total marks: 100
3. Multilevel Inheritance.
Multilevel inheritance is a type of inheritance where a class is derived from a class that is also derived from another class. This forms a chain of inheritance where a child inherits from a parent, and then another child inherits from that child, and so on.
Syntax
class A {
// Base class
};
class B : public A {
// Derived from A
};
class C : public B {
};

Write a C++ program using multilevel inheritance to create a class hierarchy where Student contains the roll number, Test contains marks in two subjects, and Result displays the total marks.
#include <iostream>
using namespace std;
// Base class
class Student {
protected:
int rollNo;
public:
void getRollNo(int r) {
rollNo = r;
}
void displayRollNo() {
cout << "Roll Number: " << rollNo << endl;
}
};
// Derived from Student
class Test : public Student {
protected:
float marks1, marks2;
public:
void getMarks(float m1, float m2) {
marks1 = m1;
marks2 = m2;
}
void displayMarks() {
cout << "Marks 1: " << marks1 << ", Marks 2: " << marks2 << endl;
}
};
// Derived from Test
class Result: public Test {
public:
void displayResult() {
displayRollNo();
displayMarks();
cout << "Total Marks: " << (marks1 + marks2) << endl;
}
};
int main() {
Result r1;
r1.getRollNo(101);
r1.getMarks(85.5, 90);
r1.displayResult();
return 0;
}Create a multilevel inheritance example in C++ where Person contains a name and age, Employee contains an employee ID, and Manager contains a department. Display all information.
#include <iostream>
using namespace std;
// Base class
class Person {
protected:
string name;
int age;
public:
void setPersonDetails(string n, int a) {
name = n;
age = a;
}
};
// Derived from Person
class Employee : public Person {
protected:
int empID;
public:
void setEmployeeDetails(int id) {
empID = id;
}
};
// Derived from Employee
class Manager : public Employee {
private:
string department;
public:
void setManagerDetails(string dept) {
department = dept;
}
void displayDetails() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Employee ID: " << empID << endl;
cout << "Department: " << department << endl;
}
};
int main() {
Manager m;
m.setPersonDetails("Basant", 35);
m.setEmployeeDetails(1001);
m.setManagerDetails("IT");
m.displayDetails();
return 0;
}Write a C++ program using multilevel inheritance where Animal has a method to say it is an animal, Mammal adds another message, and Human provides a specific identity.
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void identity() {
cout << "I am an animal." << endl;
}
};
// Derived from Animal
class Mammal : public Animal {
public:
void mammalIdentity() {
cout << "I am a mammal." << endl;
}
};
// Derived from Mammal
class Human : public Mammal {
public:
void humanIdentity() {
cout << "I am a human." << endl;
}
};
int main() {
Human h;
h.identity();
h.mammalIdentity();
h.humanIdentity();
return 0;
}</pre
4. Hierarchical Inheritance
Hierarchical inheritance is a type of inheritance in C++ where multiple derived (child) classes inherit from a single base (parent) class.
In hierarchical inheritance, many child classes come from one parent class. For example, both cars and buses come from the vehicle class.”
class A {
// members of base class
};
class B : public A{
// members of derived class 1
};
class C : public A {
// members of derived class 2
};

Write a C++ program using hierarchical inheritance where the base class is Shape, and two derived classes are Circle and Rectangle. Each derived class should calculate its area.
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
void displayType() {
cout << "This is a shape." << endl;
}
};
// Derived class 1
class Circle : public Shape {
private:
float radius;
public:
void getData() {
cout << "Enter radius of circle: ";
cin >> radius;
}
void displayArea() {
float area = 3.14 * radius * radius;
cout << "Area of Circle: " << area << endl;
}
};
// Derived class 2
class Rectangle : public Shape {
private:
float length, width;
public:
void getData() {
cout << "Enter length and width of rectangle: ";
cin >> length >> width;
}
void displayArea() {
float area = length * width;
cout << "Area of Rectangle: " << area << endl;
}
};
int main() {
Circle c;
Rectangle r;
cout << "--- Circle ---" << endl;
c.displayType();
c.getData();
c.displayArea();
cout << "\n--- Rectangle ---" << endl;
r.displayType();
r.getData();
r.displayArea();
return 0;
}</pre
Create a C++ program using hierarchical inheritance where Person is the base class and Student and Teacher are derived classes. Input and display their respective details.
#include <iostream> using namespace std; // Base class class Person { protected: string name; int age; public: void getData() { cout << "Enter name: "; cin >> name; cout << "Enter age: "; cin >> age; } void displayData() { cout << "Name: " << name << ", Age: " << age << endl; } }; // Derived class 1 class Student : public Person { private: int rollNo; public: void getData() { Person::getData(); cout << "Enter roll number: "; cin >> rollNo; } void displayData() { Person::displayData(); cout << "Roll Number: " << rollNo << endl; } }; // Derived class 2 class Teacher : public Person { private: string subject; public: void getData() { Person::getData(); cout << "Enter subject taught: "; cin >> subject; } void displayData() { Person::displayData(); cout << "Subject: " << subject << endl; } }; int main() { Student s; Teacher t; cout << "-- Student --" << endl; s.getData(); s.displayData();cout << "\n-- Teacher --" << endl; t.getData(); t.displayData(); return 0; }
5. Hybrid Inheritance:
Hybrid inheritance is a combination of two or more types of inheritance, such as single, multiple, multilevel, or hierarchical. It means a class can be derived using a mix of these forms.
Syntax
class A {
};
class B : public A {
};
class C public A{
};
class D : public B, public C {
};

Write a C++ program using hybrid inheritance to show student marks and sports score.
Use the following class structure:
- student’s class stores the name and roll number.
- Mark’s class inherits from students and stores subject marks.
- Sport class independently stores sports scores.
- Results class inherits from both Marks and Sports and displays the total result.
#include <iostream>
using namespace std;
// ----- Base class -----
class Student {
protected:
string name;
int roll;
public:
void getStudentData() {
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> roll;
}
void displayStudentData() {
cout << "\nName: " << name << "\nRoll Number: " << roll << endl;
}
};
// ----- Derived from Student -----
class Marks : public Student {
protected:
int mark1, mark2;
public:
void getMarks() {
getStudentData();
cout << "Enter marks of subject 1: ";
cin >> mark1;
cout << "Enter marks of subject 2: ";
cin >> mark2;
}
void displayMarks() {
cout << "Subject 1 Marks: " << mark1 << endl;
cout << "Subject 2 Marks: " << mark2 << endl;
}
};
// ----- Independent class -----
class Sports {
protected:
int score;
public:
void getScore() {
cout << "Enter sports score: ";
cin >> score;
}
void displayScore() {
cout << "Sports Score: " << score << endl;
}
};
// ----- Hybrid Inherited class -----
class Result : public Marks, public Sports {
public:
void getResultData() {
getMarks();
getScore();
}
void displayResult() {
displayStudentData();
displayMarks();
displayScore();
int total = mark1 + mark2 + score;
cout << "Total Score: " << total << endl;
}
};
// ----- Main Function -----
int main() {
Result r;
r.getResultData();
r.displayResult();
return 0;
}
output of prorram
Enter name: John
Enter roll number: 12
Enter marks of subject 1: 75
Enter marks of subject 2: 80
Enter sports score: 20
Name: John
Roll Number: 12
Subject 1 Marks: 75
Subject 2 Marks: 80
Sports Score: 20
Total Score: 175
Function Overriding in Inheritance.
Function overriding means defining a function in the child class with the same name and parameters as in the parent class to change its behavior.
Function overriding happens when a child class provides its own version of a function that is already defined in the parent class, using the same name and parameters.
Write a C++ program to demonstrate function overriding. Create a base class Shape with a function area(). Create a derived class Rectangle() that overrides the area()Â function to calculate the area of a rectangle.
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
void area() {
cout << "This is the area of a shape." << endl;
}
};
// Derived class
class Rectangle : public Shape {
public:
void area() {
int length = 5, width = 4;
cout << "Area of rectangle: " << length * width << endl;
}
};
int main() {
Rectangle rect;
rect.area(); // Calls the overridden function in the derived class
return 0;
}
Advantages of Inheritance
- Reuses Code: You don’t have to rewrite the same code. The child class can use the parent class’s methods and properties, saving time and effort.
- Saves Time: By reusing existing code, you build programs faster since you’re not starting from scratch.
- Easy to Maintain: If you update the parent class, all child classes get the changes automatically, making it easier to fix or improve code in one place.
- Organizes Code: Inheritance creates a clear structure, like a family tree, where related classes are grouped, making the code easier to understand.
- Extends Functionality: Child classes can add new features or modify the parent’s behavior while keeping the original code intact.
Virtual Base Class:
When a class is inherited by multiple classes, and then those classes are again inherited by another class, the original base class is copied more than once. This causes confusion or errors. To solve this problem, we make the base class a virtual base class.A virtual base class in C++ is a way to solve the “diamond problem” that can occur in multiple inheritance.

Declaration of virtual Base class
class A {
public:
void show() {
cout << "I am A";
}
};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};Program based on a virtual base class

#include <iostream>
using namespace std;
// Virtual Base Class
class student {
protected:
int r_no;
public:
void get_n() {
cout << "Enter Roll Number: ";
cin >> r_no;
}
void put_n() {
cout << "Roll Number: " << r_no << endl;
}
};
// Inherited with virtual
class test : virtual public student {
protected:
float part1, part2;
public:
void get_m() {
cout << "Enter marks for part1 and part2: ";
cin >> part1 >> part2;
}
void put_m() {
cout << "Marks - Part1: " << part1 << ", Part2: " << part2 << endl;
}
};
class sports : virtual public student {
protected:
float score;
public:
void get_s() {
cout << "Enter sports score: ";
cin >> score;
}
void put_s() {
cout << "Sports Score: " << score << endl;
}
};
// Result class inherits from both test and sports
class result : public test, public sports {
float total;
public:
void show() {
total = part1 + part2 + score;
put_n(); // from student
put_m(); // from test
put_s(); // from sports
cout << "Total Score: " << total << endl;
}
};
// Main function
int main() {
result r;
r.get_n(); // from student
r.get_m(); // from test
r.get_s(); // from sports
r.show(); // display all
return 0;
}
OutPut of program:
Enter Roll Number: 101
Enter marks for part1 and part2: 45 40
Enter sports score: 10
Roll Number: 101
Marks - Part1: 45, Part2: 40
Sports Score: 10
Total Score: 95


#include <iostream>
using namespace std;
// Base class
class Person {
protected:
string name;
int age;
public:
void getData() {
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
void displayData() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
// Derived class 1
class Student : public Person {
private:
int rollNo;
public:
void getData() {
Person::getData();
cout << "Enter roll number: ";
cin >> rollNo;
}
void displayData() {
Person::displayData();
cout << "Roll Number: " << rollNo << endl;
}
};
// Derived class 2
class Teacher : public Person {
private:
string subject;
public:
void getData() {
Person::getData();
cout << "Enter subject taught: ";
cin >> subject;
}
void displayData() {
Person::displayData();
cout << "Subject: " << subject << endl;
}
};
int main() {
Student s;
Teacher t;
cout << "-- Student --" << endl;
s.getData();
s.displayData();cout << "\n-- Teacher --" << endl;
t.getData();
t.displayData();
return 0;
}