Hierarchical inheritance

Hierarchical inheritance:

In hierarchical inheritance two or more than two derived classes inherits from a single base class.

heirachi

Example
#include<iostream.h>

class Symmetry

{

protected: float width, height;

public: void set_data (float a, float b)

{

width = a; height = b;

}

};

class Rectangle: public Symmetry

{

public: float area ()

{

return (width * height);

}

};

class Triangle: public Symmetry

{

public: float area ()

{

return (width * height / 2);

}

};

int main ()

{

Rectangle rect;

Triangle tri; rect.set_data (5,4);

tri.set_data (2,6);

cout << rect.area() << endl;

cout << tri.area() << endl; return 0;

}

Output
20

6

 

Leave a Reply

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