Inheritance is an important concept of Object-Oriented Programming (OOP). It allows one class to acquire properties and behaviors from another class. The existing class is called the base class, and the new class is called the derived class.
Inheritance allows a derived class to reuse accessible members of a base class.
class Animal {
public:
void eat() {
std::cout << "Eating";
}
};
class Dog : public Animal {
};
Here, Dog inherits from Animal.
The class whose members are inherited is called the base class, also known as the parent class.
class Animal {
public:
void eat() {
std::cout << "Eating";
}
};
In this example, Animal is the base class.
The class that inherits from another class is called the derived class, also known as the child class.
class Dog : public Animal {
};
Here, Dog is the derived class.
The basic syntax for inheritance is:
class DerivedClass : accessSpecifier BaseClass {
// derived class members
};
Example:
class Dog : public Animal {
};
#include <iostream>
class Animal {
public:
void eat() {
std::cout <<
"Animal is eating";
}
};
class Dog : public Animal {
public:
void bark() {
std::cout <<
"Dog is barking";
}
};
int main() {
Dog dog;
dog.eat();
dog.bark();
return 0;
}
The Dog object can use the inherited eat()
function as well as its own bark() function.
Inheritance allows common functionality to be written once in a base class and reused by derived classes.
class Vehicle {
public:
void start() {
std::cout << "Vehicle started";
}
};
class Car : public Vehicle {
};
class Bike : public Vehicle {
};
Both Car and Bike can use the inherited
start() function.
class Vehicle {
public:
void start() {
std::cout <<
"Vehicle started";
}
};
class Car : public Vehicle {
public:
void openTrunk() {
std::cout <<
"Trunk opened";
}
};
The derived class can add its own data members and functions.
When one derived class inherits from one base class, it is called single inheritance.
class Animal {
};
class Dog : public Animal {
};
This is the simplest form of inheritance.
When a class is derived from another derived class, it creates a chain of inheritance called multilevel inheritance.
class Animal {
};
class Mammal : public Animal {
};
class Dog : public Mammal {
};
Here:
Animal → Mammal → Dog
When one class inherits from more than one base class, it is called multiple inheritance.
class Father {
public:
void fatherFeature() {
std::cout <<
"Father feature";
}
};
class Mother {
public:
void motherFeature() {
std::cout <<
"Mother feature";
}
};
class Child :
public Father,
public Mother {
};
The Child class inherits from both Father and
Mother.
When multiple derived classes inherit from the same base class, it is called hierarchical inheritance.
class Animal {
public:
void eat() {
std::cout <<
"Eating";
}
};
class Dog : public Animal {
};
class Cat : public Animal {
};
Both Dog and Cat inherit from
Animal.
Hybrid inheritance is a combination of two or more inheritance forms.
For example, a program may combine multiple inheritance with multilevel or hierarchical inheritance.
class A {
};
class B : public A {
};
class C : public A {
};
class D :
public B,
public C {
};
Such designs need careful consideration because multiple inheritance can introduce ambiguity.
With public inheritance, public members of the base class remain public members of the derived class.
class Animal {
public:
void eat() {
std::cout << "Eating";
}
};
class Dog : public Animal {
};
A Dog object can call eat().
Private members of a base class are not directly accessible inside the derived class.
class Animal {
private:
int age;
public:
void setAge(int a) {
age = a;
}
};
class Dog : public Animal {
public:
void test() {
// age = 10; // Not allowed
}
};
The derived class can use appropriate public or protected functions provided by the base class.
Protected members can be accessed inside the class and its derived classes.
class Animal {
protected:
int age;
};
class Dog : public Animal {
public:
void setAge(int a) {
age = a;
}
};
Here, Dog can access the protected member
age.
| Base Member | Public Inheritance |
|---|---|
| public | Remains public in the derived class. |
| protected | Remains protected in the derived class. |
| private | Not directly accessible in the derived class. |
This table describes the usual access effect of public inheritance.
class Animal {
public:
void eat() {
std::cout <<
"Animal eats";
}
};
class Dog : public Animal {
public:
void bark() {
std::cout <<
"Dog barks";
}
};
int main() {
Dog dog;
dog.eat();
dog.bark();
return 0;
}
The derived object can call accessible inherited members directly.
A derived class constructor can initialize the base class by using the base class constructor in its initializer list.
class Person {
protected:
std::string name;
public:
Person(
std::string n
)
: name(n) {
}
};
class Student : public Person {
public:
Student(
std::string n
)
: Person(n) {
}
};
When a derived object is created, the base class constructor runs before the derived class constructor.
When the derived object is destroyed, the derived destructor runs before the base destructor.
class Base {
public:
Base() {
std::cout <<
"Base constructor"
<< std::endl;
}
virtual ~Base() {
std::cout <<
"Base destructor"
<< std::endl;
}
};
class Derived : public Base {
public:
Derived() {
std::cout <<
"Derived constructor"
<< std::endl;
}
~Derived() {
std::cout <<
"Derived destructor"
<< std::endl;
}
};
class Person {
protected:
std::string name;
};
class Student : public Person {
private:
int marks;
public:
void setData(
std::string n,
int m
) {
name = n;
marks = m;
}
void display() {
std::cout <<
name << " "
<< marks;
}
};
The derived class can use accessible inherited data and its own data.
#include <iostream>
#include <string>
class Person {
protected:
std::string name;
int age;
public:
Person(
std::string n,
int a
)
: name(n),
age(a) {
}
};
class Student : public Person {
private:
float marks;
public:
Student(
std::string n,
int a,
float m
)
: Person(n, a),
marks(m) {
}
void display() {
std::cout <<
"Name: "
<< name
<< std::endl;
std::cout <<
"Age: "
<< age
<< std::endl;
std::cout <<
"Marks: "
<< marks;
}
};
int main() {
Student student(
"Amit",
20,
88.5
);
student.display();
return 0;
}
A derived class can provide its own implementation of a base class function. When working with virtual functions, this is called overriding.
class Animal {
public:
virtual void sound() {
std::cout <<
"Animal sound";
}
};
class Dog : public Animal {
public:
void sound() override {
std::cout <<
"Dog barks";
}
};
The override keyword helps the compiler check that the
derived function overrides a virtual base function.
class Printer {
public:
void print() {
std::cout <<
"Printing";
}
};
class Scanner {
public:
void scan() {
std::cout <<
"Scanning";
}
};
class AllInOne :
public Printer,
public Scanner {
};
int main() {
AllInOne machine;
machine.print();
machine.scan();
return 0;
}
The derived class inherits functionality from both base classes.
Multiple inheritance can create a diamond problem when two classes inherit from the same base class and another class inherits from both of them.
class A {
};
class B : public A {
};
class C : public A {
};
class D :
public B,
public C {
};
Here, D can contain two inheritance paths to
A. Virtual inheritance can be used in suitable designs
to avoid duplicated base subobjects.
class A {
public:
int value;
};
class B : virtual public A {
};
class C : virtual public A {
};
class D :
public B,
public C {
};
Virtual inheritance makes the common base class subobject shared in the diamond hierarchy.
public inheritance when the derived type should be usable as the base type.protected functions or public interfaces over exposing unnecessary data.override when overriding virtual functions.| Concept | Meaning |
|---|---|
| Inheritance | A mechanism through which a class derives accessible members from another class. |
| Base Class | The class whose members are inherited. |
| Derived Class | The class that inherits from another class. |
| Single Inheritance | One derived class inherits from one base class. |
| Multilevel Inheritance | Inheritance occurs through multiple levels. |
| Multiple Inheritance | One class inherits from multiple base classes. |
| Hierarchical Inheritance | Multiple derived classes inherit from one base class. |
| Virtual Inheritance | Helps manage shared base subobjects in certain multiple-inheritance hierarchies. |
class Animal {
public:
virtual void sound() {
std::cout <<
"Animal sound";
}
virtual ~Animal() = default;
};
class Dog : public Animal {
public:
void sound() override {
std::cout <<
"Dog barks";
}
};
int main() {
Dog dog;
dog.sound();
return 0;
}
Question: What is the main purpose of inheritance in C++?