Hierarchical Inheritance in C++

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ย 

 

ย 

Leave a Reply

Your email address will not be published. Required fields are marked *