Table of Contents
ToggleInheritance in C++:Â
Inheritance in C++ is one of the most important concepts of object-oriented programming (OOP). It allows a class to acquire the properties and member functions of another class. Inheritance promotes code reusability, reduces code duplication, and makes programs easier to organize and maintain.
In this tutorial, you will learn the definition of inheritance in C++, its syntax, different types of inheritance, practical C++ programs with output, advantages, disadvantages, and frequently asked questions. This guide is useful for computer engineering students, beginners, and C++ interview preparation.
What Is Inheritance in C++?
Inheritance in C++ is an object-oriented programming feature in which a new class derives properties and member functions from an existing class. The existing class is called the base class or parent class, while the new class is called the derived class or child class.
For example, consider a Vehicle class with common properties such as speed and a function to start the engine. A Car class can inherit these features from Vehicle and add its own features, such as the number of doors. This approach avoids rewriting the same functionality in multiple classes.
Inheritance is widely used in software development because it helps programmers create reusable, organized, and extensible code.
Basic Terminology of Inheritance
| Term | Meaning |
|---|---|
| Base class | The class whose accessible members are inherited by another class is called base class. |
| Derived class | The class that inherits from a base class is called drived class.. |
| Parent class | Another name for a base class. |
| Child class | Another name for a derived class. |
| Reusability | Using existing code in a new class instead of rewriting it. |
| Access specifier | Controls how inherited members are accessed. |
Base class: A base class is a class whose accessible data members and member functions can be inherited by another class. It provides common properties and behaviors that can be reused by derived classes. A base class is also known as a parent class or superclass. In C++, inheritance allows a derived class to use the accessible members of a base class without rewriting the same code, which improves code reusability and reduces duplication.
Derived Class in C++: A derived class is a class that inherits accessible data members and member functions from another class, called the base class. It can reuse the properties and behaviors of the base class and can also define its own data members and member functions. A derived class is also known as a child class or subclass. In C++, inheritance helps improve code reusability and reduces code duplication.
Parent Class in C++
A parent class is a class whose accessible data members and member functions are inherited by another class. It provides common properties and behaviors that can be reused by its child classes. A parent class is also known as a base class or superclass.
Child Class in C++
A child class is a class that inherits accessible data members and member functions from another class, called the parent class. It can use inherited members and define its own additional data members and member functions. A child class is also known as a derived class or subclass.
Syntax of Inheritance in C++
The general syntax of inheritance in C++ is:
class BaseClass {
// Data members and member functions
};
class DerivedClass : access_specifier BaseClass {
// Additional members
};
Here, BaseClass is the parent class, and DerivedClass is the child class. The access specifier can be public, protected, or private. It determines how the base class’s public and protected members are accessible through the derived class.
For a simple inheritance relationship, programmers commonly use public inheritance:
class Animal {
public:
void eat() {
cout << "Animal is eating." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking." << endl;
}
};
The Dog class can use the inherited eat() function and its own bark() function.
Types of Inheritance in C++
C++ supports five commonly taught types of inheritance:
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
Each type represents a different relationship between base and derived classes.
1. Single Inheritance in C++

Single inheritance occurs when one derived class inherits from one base class. It is the simplest type of inheritance in C++. Declarations
class A {
};
class B : public A {
};Example of Single Inheritance :: Question: Write a C++ program to demonstrate single inheritance in which the Animal class has an eat() function and the Dog class has a bark() function. Display both messages using an object of the Dog class.
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal is eating." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking." << endl;
}
};
int main() {
Dog d;
d.eat();
d.bark();
return 0;
}
Output:
Animal is eating.
Dog is barking.
Explanation ::Â In this program, Animal is the base class, and Dog is the derived class. The Dog class inherits the public eat() function from Animal and defines its own bark() function. Therefore, the object d can call both functions.
2. Multiple Inheritance in C++
Multiple inheritance occurs when one derived class inherits from two or more base classes. It allows a class to combine functionality from different classes.

class A {
};
class B {
};
class C : public A, public B {
};
Example of Multiple Inheritance :: Question: Write a C++ program to demonstrate multiple inheritance using Father and Mother as base classes and Child as a derived class. Include a member function in each class to display a message.
#include <iostream>
using namespace std;
class Father {
public:
void showFather() {
cout << "This is the father class." << endl;
}
};
class Mother {
public:
void showMother() {
cout << "This is the mother class." << endl;
}
};
class Child : public Father, public Mother {
public:
void showChild() {
cout << "This is the child class." << endl;
}
};
int main() {
Child c;
c.showFather();
c.showMother();
c.showChild();
return 0;
}
Output:
This is the father class.
This is the mother class.
This is the child class.
Explanation:: The Child class inherits from both Father and Mother. As a result, its object can access the public functions of both base classes, along with its own function.
Multiple inheritance is useful when a class needs functionality from more than one source. However, it can introduce ambiguity when base classes contain members with the same name.
3. Multilevel Inheritance in C++
Multilevel inheritance occurs when a class derives from another derived class. In this structure, inheritance forms a chain of classes.For example:
Animal → Mammal → Dog
Example of Multilevel Inheritance
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal is eating." << endl;
}
};
class Mammal : public Animal {
public:
void walk() {
cout << "Mammal can walk." << endl;
}
};
class Dog : public Mammal {
public:
void bark() {
cout << "Dog is barking." << endl;
}
};
int main() {
Dog d;
d.eat();
d.walk();
d.bark();
return 0;
}
Output:
Animal is eating.
Mammal can walk.
Dog is barking.
Explanation :: The Mammal class inherits from Animal, and the Dog class inherits from Mammal. Therefore, an object of Dog can access the public functions inherited through the chain, as well as its own function.
4. Hierarchical Inheritance in C++
Hierarchical inheritance occurs when two or more derived classes inherit from the same base class. It allows several classes to reuse common functionality. For example:
Animal
/ \
Dog Cat
Example of Hierarchical Inheritance
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Animal is eating." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog is barking." << endl;
}
};
class Cat : public Animal {
public:
void meow() {
cout << "Cat is meowing." << endl;
}
};
int main() {
Dog d;
Cat c;
d.eat();
d.bark();
c.eat();
c.meow();
return 0;
}
Output:
Animal is eating.
Dog is barking.
Animal is eating.
Cat is meowing.
Explanation:: Both Dog and Cat inherit the eat() function from Animal. Each derived class also has its own specific function. This reduces duplication because the common functionality is written only once in the base class.
5. Hybrid Inheritance in C++
Hybrid inheritance is a combination of two or more types of inheritance. For example, it may combine hierarchical and multiple inheritance.
A common example is the diamond structure, in which two classes inherit from the same base class and another class inherits from both of them.
Example of Hybrid Inheritance
#include <iostream>
using namespace std;
class Person {
public:
void showPerson() {
cout << "This is a person." << endl;
}
};
class Student : virtual public Person {
public:
void showStudent() {
cout << "This is a student." << endl;
}
};
class Employee : virtual public Person {
public:
void showEmployee() {
cout << "This is an employee." << endl;
}
};
class TeachingAssistant : public Student, public Employee {
public:
void showRole() {
cout << "Teaching assistant role." << endl;
}
};
int main() {
TeachingAssistant t;
t.showPerson();
t.showStudent();
t.showEmployee();
t.showRole();
return 0;
}
Output:
This is a person.
This is a student.
This is an employee.
Teaching assistant role.
Explanation::Â The Student and Employee classes both inherit from Person, and TeachingAssistant inherits from both classes. The keyword virtual makes the two intermediate classes share a single Person base-class subobject in the final derived object. This avoids the duplicated Person base that would otherwise occur in this diamond structure.
6. Multilevel Inheritance with Student Details
The following example demonstrates how inheritance can be used in a simple student information program.
#include <iostream>
using namespace std;
class Person {
protected:
string name;
public:
void setName(string n) {
name = n;
}
};
class Student : public Person {
protected:
int roll;
public:
void setRoll(int r) {
roll = r;
}
};
class Result : public Student {
private:
float marks;
public:
void setMarks(float m) {
marks = m;
}
void display() {
cout << "Name: " << name << endl;
cout << "Roll: " << roll << endl;
cout << "Marks: " << marks << endl;
}
};
int main() {
Result r;
r.setName("Ram");
r.setRoll(101);
r.setMarks(85.5);
r.display();
return 0;
}
Output:
Name: Ram
Roll: 101
Marks: 85.5
Explanation :: The Person class stores the student’s name, the Student class adds a roll number, and the Result class stores marks and displays the complete information. The protected members can be accessed by derived classes but cannot be accessed directly from ordinary external code through the object.
7. Public, Protected, and Private Inheritance
C++ provides three inheritance access modes: public, protected, and private. These control how inherited public and protected members are exposed in the derived class.
| Base class member | Public inheritance | Protected inheritance | Private inheritance |
|---|---|---|---|
| Public | Public | Protected | Private |
| Protected | Protected | Protected | Private |
| Private | Not directly accessible | Not directly accessible | Not directly accessible |
Private base-class members still exist as part of the base-class subobject, but derived classes cannot access them directly by name. They may be accessed through suitable public or protected base-class member functions.
Example of Public Inheritance
#include <iostream>
using namespace std;
class Base {
public:
int x = 10;
protected:
int y = 20;
};
class Derived : public Base {
public:
void display() {
cout << "x = " << x << endl;
cout << "y = " << y << endl;
}
};
int main() {
Derived d;
d.display();
cout << "x = " << d.x << endl;
return 0;
}
Output:
x = 10
y = 20
x = 10
The derived class can access both x and y, but external code can directly access only the public member x. The protected member y cannot be accessed directly through d.
Advantages of Inheritance in C++
Inheritance provides several benefits in object-oriented software development.
- Code reusability: Existing functionality can be reused without rewriting it in every derived class.
- Reduced code duplication: Common data and functions can be maintained in a shared base class.
- Easy maintenance: Updating shared functionality in a base class can benefit derived classes.
- Extensibility: New classes can extend existing functionality with additional members.
- Better organization: Related classes can be arranged in a logical hierarchy.
- Support for polymorphism: Inheritance works with virtual functions to enable runtime polymorphism.
- Improved consistency: Common behavior can be implemented in a single place.
Disadvantages of Inheritance in C++
Although inheritance is useful, it also has some limitations.
- Increased complexity: Deep or complicated class hierarchies can be difficult to understand.
- Tight coupling: Changes to a base class can affect derived classes.
- Ambiguity: Multiple inheritance can create naming conflicts when base classes contain members with the same name.
- Design limitations: Inheritance may be unsuitable when classes do not have a genuine is-a relationship.
- Debugging difficulties: Errors involving several levels of inheritance may be harder to trace.
- Overuse: Excessive inheritance can make code less flexible than a design based on composition.
Difference Between Inheritance and Polymorphism
Inheritance and polymorphism are related OOP concepts, but they serve different purposes.
| Inheritance | Polymorphism |
|---|---|
| Allows a class to derive from another class. | Allows an interface or operation to have different behavior in different contexts. |
| Supports code reuse and class hierarchies. | Supports flexible behavior and implementation selection. |
| Can exist without runtime polymorphism. | Runtime polymorphism commonly uses inheritance and virtual functions in C++. |
Example: Dog inherits from Animal. | Example: a virtual sound() function behaves differently for Dog and Cat. |
Read more: Polymorphism in C++.
Inheritance vs. Composition in C++
Inheritance represents an is-a relationship, while composition represents a has-a relationship.
For example, a Dog is an Animal, so inheritance can be appropriate. A Car has an Engine, so composition may be more suitable.
Inheritance should be used when the derived class genuinely represents a specialized form of the base class. Composition is often preferable when one object simply needs to use the services of another object.
Common Mistakes in C++ Inheritance
Beginners should avoid the following mistakes:
- Forgetting the colon before the inheritance access specifier.
- Using private base-class members directly inside a derived class.
- Confusing public inheritance with public access to every inherited member.
- Creating complicated multiple-inheritance structures without considering ambiguity.
- Assuming inheritance automatically creates runtime polymorphism.
- Forgetting to use virtual functions when runtime polymorphism is required.
- Using inheritance when composition better represents the relationship.
Frequently Asked Questions (FAQs)
1. What is inheritance in C++?
Inheritance is an OOP feature that allows a derived class to acquire accessible members and behavior from a base class. It supports code reuse and the creation of related class hierarchies.
2. What are the five types of inheritance in C++?
The five commonly taught types are single, multiple, multilevel, hierarchical, and hybrid inheritance.
3. What is a base class in C++?
A base class is an existing class from which another class derives. It is also called a parent class or superclass.
4. What is a derived class in C++?
A derived class is a class that inherits from one or more base classes. It can add new members and use accessible inherited members.
5. Can a class inherit from multiple classes in C++?
Yes. C++ supports multiple inheritance, which allows a derived class to inherit from more than one base class.
6. What is the difference between public and private inheritance?
Public inheritance preserves public base members as public and protected members as protected. Private inheritance makes both inherited public and protected members private within the derived class.
7. Are private members inherited in C++?
The base-class subobject, including its private members, is part of a derived object. However, private base-class members cannot be accessed directly by name from the derived class.
8. What is hybrid inheritance?
Hybrid inheritance combines two or more inheritance patterns. A diamond-shaped class hierarchy is one common example.
9. What is the diamond problem in C++?
The diamond problem occurs when a class inherits from two classes that share the same base class. Without virtual inheritance, the final derived class can contain two separate base-class subobjects, which may cause ambiguity.
10. What is the main advantage of inheritance?
The main advantage is code reusability. It also helps organize related classes and supports runtime polymorphism when used with virtual functions.
C++ Inheritance Practice Questions
Test your understanding with the following questions.
Short-answer questions
- Define inheritance in C++.
- What is the difference between a base class and a derived class?
- List the five common types of inheritance.
- What is the purpose of the
protectedaccess specifier? - What is multiple inheritance?
- Explain the difference between inheritance and composition.
Programming questions
Write a C++ program to demonstrate single inheritance using
AnimalandDog.Write a program to demonstrate multiple inheritance using
Father,Mother, andChild.Write a C++ program to demonstrate multilevel inheritance using
Person,Student, andResult.Write a program to demonstrate hierarchical inheritance using
Shape,Circle, andRectangle.Write a program that demonstrates the diamond problem and solves it using virtual inheritance.
C++ interview questions
- Why is inheritance used in C++?
- What is the difference between public, protected, and private inheritance?
- Can a derived class directly access private members of a base class?
- What is the difference between inheritance and polymorphism?
- What is virtual inheritance, and when is it useful?
- What happens when two base classes contain functions with the same name?
- When should composition be preferred over inheritance?
Conclusion
Inheritance in C++ is a fundamental concept of object-oriented programming that allows classes to reuse and extend existing functionality. The five common types—single, multiple, multilevel, hierarchical, and hybrid inheritance—provide different ways to organize relationships between classes.
By understanding inheritance syntax, access specifiers, practical programs, and the differences between inheritance and composition, you can write more reusable and maintainable C++ programs. Practice the examples in a C++ compiler and experiment with your own base and derived classes to strengthen your understanding.
Related C++ Tutorials
Continue learning with these tutorials on BasantYadav.com.np:
Author: Er. Basant Kumar Yadav
Website: BasantYadav.com.np
