Inheritance is one of the fundamental principles of Object-Oriented Programming (OOP). It allows a new class to acquire the properties and behaviors of an existing class, promoting code reuse, reducing duplication, and making programs easier to maintain.
In C++, inheritance enables developers to create hierarchical relationships between classes, where a derived class extends the functionality of a base class. It is widely used in software development to build scalable and reusable applications.
Learning Objectives
Understand inheritance in C++
Create base and derived classes
Implement different types of inheritance
Understand virtual base classes
Create abstract base classes
Understand constructors and destructors in inheritance
Apply inheritance in object-oriented programs
What is Inheritance?
Inheritance allows one class to inherit the properties and functions of another class.
General syntax:
class Derived : access_specifier Base
{
// members
};
Benefits include:
Code reusability
Easier maintenance
Better scalability
Reduced code duplication
Single Inheritance
In single inheritance, one derived class inherits from one base class.
Example:
#include <iostream>
using namespace std;
class Person
{
public:
void display()
{
cout << "Person Information";
}
};
class Student : public Person
{
};
int main()
{
Student s;
s.display();
return 0;
}
Output
Person Information
Multiple Inheritance
A class inherits from more than one base class.
Example:
#include <iostream>
using namespace std;
class Father
{
public:
void father()
{
cout << "Father ";
}
};
class Mother
{
public:
void mother()
{
cout << "Mother";
}
};
class Child : public Father, public Mother
{
};
int main()
{
Child c;
c.father();
c.mother();
return 0;
}
Output
Father Mother
Multilevel Inheritance
A derived class becomes the base class for another class.
Example:
class A { };
class B : public A { };
class C : public B { };
Hierarchical Inheritance
Multiple derived classes inherit from one base class.
Example:
class Animal { };
class Dog : public Animal { };
class Cat : public Animal { };
Hybrid Inheritance
Hybrid inheritance combines two or more inheritance types.
It is commonly used in large applications but should be designed carefully to avoid ambiguity.
Virtual Base Classes
Virtual inheritance helps solve the Diamond Problem, where multiple inheritance causes duplicate copies of a base class.
Example:
class A { };
class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C { };
Virtual inheritance ensures only one instance of the base class is inherited.
Abstract Base Classes
An abstract base class contains at least one pure virtual function.
Example:
#include <iostream>
using namespace std;
class Animal
{
public:
virtual void sound() = 0;
};
class Dog : public Animal
{
public:
void sound()
{
cout << "Dog Barks";
}
};
int main()
{
Dog d;
d.sound();
return 0;
}
Output
Dog Barks
Constructors and Destructors in Inheritance
Constructors execute from the base class to the derived class.
Destructors execute in the reverse order.
Example:
#include <iostream>
using namespace std;
class Base
{
public:
Base()
{
cout << "Base Constructor\n";
}
~Base()
{
cout << "Base Destructor\n";
}
};
class Derived : public Base
{
public:
Derived()
{
cout << "Derived Constructor\n";
}
~Derived()
{
cout << "Derived Destructor";
}
};
int main()
{
Derived d;
return 0;
}
Output
Base Constructor
Derived Constructor
Derived Destructor
Base Destructor
Advantages of Inheritance
Promotes code reuse
Simplifies maintenance
Supports hierarchical design
Encourages modular programming
Extends existing functionality
Common Mistakes
Using private inheritance unintentionally.
Forgetting to declare virtual functions when needed.
Creating ambiguity in multiple inheritance.
Misunderstanding constructor execution order.
Best Practices
Prefer public inheritance for "is-a" relationships.
Use virtual inheritance only when necessary.
Keep inheritance hierarchies simple.
Use abstract classes to define common interfaces.
Avoid deep inheritance trees.
Still need help?
Contact us