C++ Multiple, Multilevel and Hierarchical Inheritance
C++ Multiple, Multilevel and Hierarchical Inheritance
Hierarchical Inheritance
In this tutorial, we will learn about different models of inheritance in C++
programming: Multiple, Multilevel and Hierarchical inheritance with examples.
class A {
... .. ...
};
class B: public A {
... .. ...
};
class C: public B {
... ... ...
};
Here, class B is derived from the base class A and the class C is derived from
the derived class B .
#include <iostream>
using namespace std;
class A {
public:
void display() {
cout<<"Base class content.";
}
};
int main() {
C obj;
obj.display();
return 0;
}
Output
In this program, class C is derived from class B (which is derived from base
class A ).
The compiler first looks for the display() function in class C . Since the function
doesn't exist there, it looks for the function in class B (as C is derived from B ).
The function also doesn't exist in class B , so the compiler looks for it in class A
Multiple Inheritance
#include <iostream>
using namespace std;
class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth." << endl;
}
};
class WingedAnimal {
public:
WingedAnimal() {
cout << "Winged animal can flap." << endl;
}
};
int main() {
Bat b1;
return 0;
}
Output
The most obvious problem with multiple inheritance occurs during function
overriding.
Suppose, two base classes have a same function which is not overridden in
derived class.
If you try to call the function using the object of the derived class, compiler shows
error. It's because compiler doesn't know which function to call. For example,
class base1 {
public:
void someFunction( ) {....}
};
class base2 {
void someFunction( ) {....}
};
class derived : public base1, public base2 {};
int main() {
derived obj;
obj.someFunction() // Error!
}
This problem can be solved using the scope resolution function to specify which
function to class either base1 or base2
int main() {
obj.base1::someFunction( ); // Function of base1 class is called
obj.base2::someFunction(); // Function of base2 class is called.
}
For example, Physics, Chemistry, Biology are derived from Science class.
Similarly, Dog, Cat, Horse are derived from Animal class.
class base_class {
... .. ...
}
#include <iostream>
using namespace std;
// base class
class Animal {
public:
void info() {
cout << "I am an animal." << endl;
}
};
// derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "I am a Dog. Woof woof." << endl;
}
};
// derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "I am a Cat. Meow." << endl;
}
};
int main() {
// Create object of Dog class
Dog dog1;
cout << "Dog Class:" << endl;
dog1.info(); // Parent Class function
dog1.bark();
return 0;
}
Run Code
Output
Dog Class:
I am an animal.
I am a Dog. Woof woof.
Cat Class:
I am an animal.
I am a Cat. Meow.
Here, both the Dog and Cat classes are derived from the Animal class. As such,
both the derived classes can access the info() function belonging to the
Animal class.