Inheritance in C++: Unlocking the Full Potential of Object-Oriented Programming
4 min readAug 2, 2023
Inheritance is a key concept in C++ object-oriented programming that allows one class (called the derived class) to inherit properties and behaviors from another class (called the base class). It enables code reuse and promotes the idea of a “is-a” relationship, where the derived class is a specialized version of the base class. In C++, inheritance is declared using the “class” keyword and the colon :symbol.
There are four types of inheritance in C++:
- Single Inheritance: A derived class can inherit from only one base class.
- Multiple Inheritance: A derived class can inherit from multiple base classes. However, multiple inheritance can lead to ambiguity issues and is generally more complex.
- Multilevel Inheritance: A derived class can inherit from another derived class, forming a chain of classes.
- Hybrid inheritance : it is a combination of multiple inheritance and single inheritance in C++. In multiple inheritance, a class can inherit from multiple base classes, and in single inheritance, a class can inherit from only one base class. Hybrid inheritance involves the use of both these inheritance types to form a complex inheritance hierarchy.To create a hybrid inheritance, you can define a class that inherits from multiple base classes, and then other classes can inherit from this derived class, creating a chain of classes that includes both single and multiple inheritance.
Example for single inheritance
#include <iostream>
// Base class
class Shape {
public:
void draw() {
std::cout << "Drawing a shape." << std::endl;
}
};
// Derived class, inheriting from Shape
class Circle : public Shape {
public:
void draw() {
std::cout << "Drawing a circle." << std::endl;
}
};
int main() {
Circle circle;
circle.draw(); // This will call the draw() function of the Circle class.
return 0;
}
Example for multiple inheritance
#include <iostream>
// First base class
class Shape {
public:
void draw() {
std::cout << "Drawing a shape." << std::endl;
}
};
// Second base class
class Color {
public:
void fill() {
std::cout << "Filling with color." << std::endl;
}
};
// Derived class, inheriting from both Shape and Color
class Circle : public Shape, public Color {
public:
void draw() {
std::cout << "Drawing a circle." << std::endl;
}
};
int main() {
Circle circle;
circle.draw(); // This will call the draw() function of the Circle class.
circle.fill(); // This will call the fill() function of the Color class.
return 0;
}
Example for multilevel inheritance
#include <iostream>
// Base class
class Shape {
public:
void draw() {
std::cout << "Drawing a shape." << std::endl;
}
};
// Intermediate class, inheriting from Shape
class Circle : public Shape {
public:
void draw() {
std::cout << "Drawing a circle." << std::endl;
}
};
// Derived class, inheriting from Circle
class ColoredCircle : public Circle {
public:
void fillWithColor() {
std::cout << "Filling the circle with color." << std::endl;
}
};
int main() {
ColoredCircle coloredCircle;
coloredCircle.draw(); // This will call the draw() function of the Circle class.
coloredCircle.fillWithColor(); // This will call the fillWithColor() function of the ColoredCircle class.
return 0;
}
Example for hybrid inheritance
#include <iostream>
class A {
public:
void functionA() {
std::cout << "Function A" << std::endl;
}
};
class B {
public:
void functionB() {
std::cout << "Function B" << std::endl;
}
};
// Multiple Inheritance: C inherits from both A and B
class C : public A, public B {
public:
void functionC() {
std::cout << "Function C" << std::endl;
}
};
// Single Inheritance: D inherits from C
class D : public C {
public:
void functionD() {
std::cout << "Function D" << std::endl;
}
};
int main() {
D d;
d.functionA(); // Function from class A
d.functionB(); // Function from class B
d.functionC(); // Function from class C
d.functionD(); // Function from class D
return 0;
}
Types of Access Specifiers in Inheritance:
- public: All public members of the base class become public members of the derived class. The inherited members retain their accessibility in the derived class.
- protected: All public and protected members of the base class become protected members of the derived class. The inherited public members become protected in the derived class.
- private : All public and protected members of the base class become private members of the derived class. The inherited members become private in the derived class and are not accessible directly from outside the class.
#include <iostream>
class Base {
public:
int public_var;
void public_function() {
std::cout << "Public function." << std::endl;
}
protected:
int protected_var;
void protected_function() {
std::cout << "Protected function." << std::endl;
}
private:
int private_var;
void private_function() {
std::cout << "Private function." << std::endl;
}
};
class DerivedPublic : public Base {
// All members of Base are inherited as public.
// public_var, public_function() are accessible directly.
};
class DerivedProtected : protected Base {
// All members of Base are inherited as protected.
// public_var, public_function() are accessible as protected.
};
class DerivedPrivate : private Base {
// All members of Base are inherited as private.
// public_var, public_function() are accessible as private.
};
int main() {
DerivedPublic derived1;
derived1.public_var = 10; // Accessible directly as public.
derived1.public_function(); // Accessible directly as public.
DerivedProtected derived2;
// derived2.public_var = 20; // Not accessible as protected.
// derived2.public_function(); // Not accessible as protected.
derived2.protected_function(); // Accessible as protected.
DerivedPrivate derived3;
// derived3.public_var = 30; // Not accessible as private.
// derived3.public_function(); // Not accessible as private.
// derived3.protected_function(); // Not accessible as private.
return 0;
}