Hierarchical Inheritance in C++
Hierarchical Inheritance in C++
ย Hierarchical Inheritance : Inheriting is a method of inheritance where one or more derived
classes is derived from common base class.
In Hierarchical inheritance, more than one sub-class inherits the property of a single base class. There is one base class and multiple derived classes. Several other classes inherit the derived classes as well. Hierarchical structures thus form a tree-like structure.
SYNTAX :
Class A { ............ }; Class B: access_specifier A { ......... }; Class C: access_specifier Aย { .............ย };
CODE :
#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();
// create object of Cat class
Cat cat1;
cout << "\nCat Class:" << endl;
cat1.info(); // parent Class function
cat1.meow();
return 0;
}
Output
Dog Class: I am an animal. I am a Dog. Woof woof. Cat Class: I am an animal. I am a Cat. Meow.
ย
Topics Covered :
- inheritance in c++
- hierarchical inheritance in c++
- inheritance example in c++
- hierarchical inheritance
- hierarchical
- c++ tutorial
Thanks for reading this blog. Hope you get satisfied with the blog and definitely this blog must have valued your time and effort of reading.
Take a time to connect our other digital creations such as Instagram , Facebook and Youtube.
ย Social Mediaย Linksย ofย Tech DCodeย :
ย YouTube :ย https://www.youtube.com/channel/UCjJnEdeugftBwQ3yMuD4B_A
ย Instagram :ย https://www.instagram.com/thetechdcode/
ย Facebook Page :ย https://www.facebook.com/thetechdcode
ย Twitter :ย https://twitter.com/thetechdcode
ย Telegram Channel :ย https://t.me/thetechdcode
ย Tech DCode Linktree :ย https://linktr.ee/thetechdcode
ย My Personal Handles :ย https://linktr.ee/virtualshivamin
ย Social Media Linksย ofย SHIVAM SINGHย (OWNER) :
ย Instagram :ย https://www.instagram.com/virtualshivamin/
ย Facebook Page :ย https://www.facebook.com/virtualshivamin/
ย Twitter :ย https://twitter.com/virtualshivamin/
ย Personal Linktree :ย https://linktr.ee/virtualshivaminย
ย