C++ Tutorial
Class and Object
Reference
Inheritance and Derivation
Polymorphism and Virtual Functions
Operator Overloading
Template
Exception
Object Oriented Advanced
Input/Output Stream
File Operations
In C++, inheritance is a way to create a new class (derived class) from an existing class (base class). The derived class inherits the properties and methods of the base class, allowing for code reuse and the creation of hierarchical class structures.
Inheritance can be implemented in three different ways: single inheritance, multiple inheritance, and multilevel inheritance.
In this tutorial, we'll cover these three ways of inheritance in C++.
In single inheritance, a derived class inherits from a single base class. This is the most common and simplest form of inheritance.
Example:
#include <iostream> class Animal { public: void eat() { std::cout << "I can eat." << std::endl; } }; class Dog : public Animal { public: void bark() { std::cout << "I can bark." << std::endl; } }; int main() { Dog dog; dog.eat(); // inherited from Animal dog.bark(); return 0; }
In this example, Dog
inherits from the Animal
class using single inheritance.
In multiple inheritance, a derived class inherits from more than one base class.
Example:
#include <iostream> class Animal { public: void eat() { std::cout << "I can eat." << std::endl; } }; class Mammal { public: void breathe() { std::cout << "I can breathe." << std::endl; } }; class Dog : public Animal, public Mammal { public: void bark() { std::cout << "I can bark." << std::endl; } }; int main() { Dog dog; dog.eat(); // inherited from Animal dog.breathe(); // inherited from Mammal dog.bark(); return 0; }
In this example, Dog
inherits from both the Animal
and Mammal
classes using multiple inheritance.
In multilevel inheritance, a derived class inherits from a base class, which in turn inherits from another class.
Example:
#include <iostream> class LivingBeing { public: void live() { std::cout << "I am a living being." << std::endl; } }; class Animal : public LivingBeing { public: void eat() { std::cout << "I can eat." << std::endl; } }; class Dog : public Animal { public: void bark() { std::cout << "I can bark." << std::endl; } }; int main() { Dog dog; dog.live(); // inherited from LivingBeing through Animal dog.eat(); // inherited from Animal dog.bark(); return 0; }
In this example, Dog
inherits from the Animal
class, which itself inherits from the LivingBeing
class. This is an example of multilevel inheritance.
These three ways of inheritance allow you to create complex class structures and promote code reuse in C++. Each type of inheritance has its own advantages and use cases, depending on the problem you're trying to solve.
Multiple inheritance in C++ examples:
#include <iostream> // Base class 1 class Base1 { public: void display1() { std::cout << "Base1 Display" << std::endl; } }; // Base class 2 class Base2 { public: void display2() { std::cout << "Base2 Display" << std::endl; } }; // Derived class inheriting from Base1 and Base2 class Derived : public Base1, public Base2 { public: void displayDerived() { std::cout << "Derived Display" << std::endl; } }; int main() { Derived obj; obj.display1(); // Calls display1 from Base1 obj.display2(); // Calls display2 from Base2 obj.displayDerived(); // Calls displayDerived from Derived return 0; }
Multilevel inheritance in C++:
#include <iostream> // Base class class Base { public: void displayBase() { std::cout << "Base Display" << std::endl; } }; // Derived class from Base class Derived1 : public Base { public: void displayDerived1() { std::cout << "Derived1 Display" << std::endl; } }; // Further derived class from Derived1 class Derived2 : public Derived1 { public: void displayDerived2() { std::cout << "Derived2 Display" << std::endl; } }; int main() { Derived2 obj; obj.displayBase(); // Calls displayBase from Base obj.displayDerived1(); // Calls displayDerived1 from Derived1 obj.displayDerived2(); // Calls displayDerived2 from Derived2 return 0; }