15 Sept 2019

  • September 15, 2019
  • Amitraj

4.)  Hierarchical - Inheritance 

Inheritance is the process of inheriting properties of objects of one class by objects of another class. The class which inherits the properties of another class is called Derived or Child or Sub class and the class whose properties are inherited is called Base or Parent or Super class. When more than one classes are derived from a single base class, such inheritance is known as Hierarchical Inheritance, where features that are common in lower level are included in parent class. Problems where hierarchy has to be maintained can be solved easily using this inheritance.

http://codevidyalay.blogspot.com

For example:-

 1.) Civil, Computer, Mechanical, Electrical are derived from Engineer.

 2.) Natural language, Programming language are derived from Language.

3.) Physics, Chemistry, Biology are derived from Science class.



Example of Hierarchical Inheritance in C++:-

// cpp program on hierarchical inheritance


#include<iostream>

using namespace std;

class A

{

 public:

  void show()

  {

      cout<<"show called..";

  }

};

class B: public A

{

 public:

  void display()

  {

   cout<<"display called..";

  }

};

class C: public A

{

 public:

  void output()

        {

         cout<<"output called..";

    } 

};

int main()

{

 B b;

 C c;

 b.display();

 b.show();

 c.output();

 c.show();

 return 0;

}

INPUT/OUTPUT:-

display called..show called..output called..show called..

Translate

Popular Posts