Inheritance
A feature of Object-Oriented Programming.
One class (child / derived class)
can acquire properties and behaviours of another class (parent/base class).
- To
avoid code duplication.
- To
improve reusability.
- To
model real-world relationships (“is-a” relationship).
- To
support polymorphism.
Basic Syntax of
Inheritance
class Base {
// data + functions
};
class Derived: access_specifier
Base {
// extra code
};
Access specifiers:
|
Specifier |
Meaning |
|
public |
Public members of base → public
in derived |
|
protected |
Public members of base →
protected in derived |
|
private |
Public members of base → private
in derived |
---------------------------------------------------------
Need for
Inheritance
---------------------------------------------------------
1. Reusability of Code
- Reuse
existing code
rather than writing it againà Common attributes and methods
are written once in the base class and reused in derived classes.
Example:
A Vehicle class contains engine, mileage, and colour.
2. Avoiding Redundancy
Without inheritance, every class
would repeat the same code.
Example:
Three student-related classes, each writing name, roll,and marks separately → Redundant
3. Implementing IS-A Relationship
Inheritance models real-world
relations.
Car IS-A Vehicle
4. Extensibility (Easy to Add
Features)
Future enhancements become easy.
5. Polymorphism Support
Inheritance is the foundation for runtime
polymorphism using:
- Virtual
functions
- Function
overriding
- Dynamic
dispatch
Without inheritance, polymorphism cannot
exist.
6. Better Code Organisation
Classes can be arranged
hierarchically:
LivingThing
├──
Animal
│ ├──
Mammal
│ ├──
Reptile
└── Plant
7. Promotes DRY Principle (Don’t
Repeat Yourself)
Inheritance inherently supports DRY
by extracting common code.
8. Allows Specialisation
Base class → general
Derived class → specialised
Example:
Shape → Circle, Rectangle, Triangle
============================================================
Types of
Inheritance in C++
1. Single Inheritance
2. Multilevel Inheritance
3. Multiple Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
6. Multipath Inheritance (special
case inside hybrid → solved by Virtual Inheritance)
--------------------------------------------------
1. SINGLE INHERITANCE
One base class → one derived class.
Simplest form: derived class extends the features of the base class.
Diagram
Base
|
Derived
Example
#include <iostream>
using namespace std;
class Person { // Base class
public:
string name;
void getData() {
cout << "Enter name: ";
cin >> name;
}
};
class Student: public Person
{ // Derived class
public:
int roll;
void getRoll() {
cout << "Enter roll: ";
cin >> roll;
}
void display() {
cout << "Name: "
<< name << ", Roll: " << roll << endl;
}
};
int main() {
Student s;
s.getData();
s.getRoll();
s.display();
}
--------------------------------------------------
2. MULTILEVEL INHERITANCE
A derived class acts as a base for
another derived class.
Diagram
Grandparent →
Parent → Child
Example
#include <iostream>
using namespace std;
class A {
public:
void showA() { cout << "Class A\n"; }
};
class B : public A {
public:
void showB() { cout << "Class B\n"; }
};
class C : public B {
public:
void showC() { cout << "Class C\n"; }
};
int main() {
C obj;
obj.showA();
obj.showB();
obj.showC();
}
--------------------------------------------------
3. MULTIPLE INHERITANCE
One derived class inherits from more
than one base class.
Diagram
Base1
\
\
Derived
/
/
Base2
Example
#include <iostream>
using namespace std;
class Teacher {
public:
void teach() { cout << "Teaching...\n"; }
};
class Researcher {
public:
void research() { cout << "Researching...\n"; }
};
class Professor: public Teacher,
public Researcher {
public:
void work() { cout << "Professor working...\n"; }
};
int main() {
Professor p;
p.teach();
p.research();
p.work();
}
--------------------------------------------------
4. HIERARCHICAL INHERITANCE
Multiple derived classes from a
single base class.
Diagram
Base
/
| \
Child1 Child2 Child3
Example
#include <iostream>
using namespace std;
class Vehicle {
public:
void start() { cout << "Vehicle starts\n"; }
};
class Car : public Vehicle {
public:
void carInfo() { cout << "I am a Car\n"; }
};
class Bike : public Vehicle {
public:
void bikeInfo() { cout << "I am a Bike\n"; }
};
int main() {
Car c;
Bike b;
c.start();
c.carInfo();
b.start();
b.bikeInfo();
}
--------------------------------------------------
5. HYBRID INHERITANCE
A combination of two or more types of
inheritance.
Common example: Hierarchical + Multiple Inheritance.
Diagram
A
/ \
B C
\ /
D
Example
#include <iostream>
using namespace std;
class A {
public:
void showA() { cout << "Class A\n"; }
};
class B : public A {
public:
void showB() { cout << "Class B\n"; }
};
class C : public A {
public:
void showC() { cout << "Class C\n"; }
};
class D : public B, public C {
public:
void showD() { cout << "Class D\n"; }
};
int main() {
D obj;
obj.showD();
}
--------------------------------------------------
6. MULTIPATH INHERITANCE → Virtual
Base Classes
Problem
When a class inherits from two
classes, both of which inherit from the same base class, the base
class data/functions are duplicated.
Solution: virtual inheritance
Diagram
A
/ \
B C
\ /
\ /
D
Example (Virtual Inheritance)
#include <iostream>
using namespace std;
class A {
public:
int x;
};
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C {
public:
void setX(int val) { x = val; }
void show() { cout << "x = " << x << endl; }
};
int main() {
D obj;
obj.setX(10);
obj.show();
}
Access Specifiers
in Inheritance
|
Base Access |
Derived class (public) |
Derived class (protected) |
Derived class (private) |
|
public |
public |
protected |
private |
|
protected |
protected |
protected |
private |
|
private |
not inherited |
not inherited |
not inherited |
Summary
- Single
inheritance –
1 base → 1 derived
- Multiple
inheritance –
derived from multiple bases
- Multilevel
inheritance –
"grandparent → parent → child"
- Hierarchical
inheritance –
one base → many derived
- Hybrid
inheritance – a combination of multiple types
- Multipath
inheritance –
same base class inherited twice → solved by virtual inheritance
==================================================================
Diamond Problem
(Multiple Inheritance Issue)
Occurs when two base classes
inherit from the same parent.
Problem Example:
A
/ \
B C
\ /
D
Solution → Use virtual inheritance
class A {
public:
int x;
};
class B : virtual public A {};
class C : virtual public A {};
class D : public B, public C {};
================================================================
0 Comments