Inheritance
Inheritance is a fundamental concept in Object-Oriented Programming (OOP). It allows one
class to reuse the properties and methods of another class. This improves code reusability,
modularity, and maintainability, which is why inheritance is widely used in real software
systems like banking apps, management systems, and game engines.
Inheritance means creating a new class (child/derived class) from an existing class (parent/base
class).
• Parent / Base Class → The class whose properties and methods are inherited.
• Child / Derived Class → The class that inherits from the parent class.
Basic Syntax
class Parent
{
};
class Child : public Parent
{
};
Here the Child class can access the functions and data members of the Parent class.
Types of Inheritance in OOP
There are five main types of inheritance:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
1. Single Inheritance
Single inheritance occurs when one child class inherits from only one parent class.
Parent
Child
Basic Structure
class Parent
};
class Child : public Parent
};
#include<iostream>
using namespace std;
class parent
{
public:
int n;
void in()
{
cout<<"You entered number";
cin>>n;
}
};
class child:public parent //derive or inherit
{
public:
void out()
{
cout<<"Your entered number is"<<n;
}
};
main()
{
child c;
[Link]();
[Link]();
}
Real Software Example
User Management System
Parent Class:
User
Child Classes may include:
• Admin
• Customer
• Employee
All users share common properties like:
• name
• email
• login()
But each child class can have additional features.
2. Multiple Inheritance
Multiple inheritance occurs when a child class inherits from more than one parent class.
Parent A Parent B
Child
Basic Structure
class A
{ };
class B
{ };
class C : public A, public B, public D
{ };
#include<iostream>
using namespace std;
class Teacher
{
public:
void teach()
{
cout<<"Teaching students"<<endl;
}
};
class Researcher
{
public:
void research()
{
cout<<"Doing research"<<endl;
}
};
class Professor : public Teacher, public Researcher
{
};
int main()
{
Professor p;
[Link]();
[Link]();
}
Real Software Example
University Management System
A Professor may have two roles:
• Teacher
• Researcher
Structure:
Teacher Researcher
Professor
The Professor class inherits both teaching and research capabilities.