Hierarchical Inheritance in C++.

Hierarchical Inheritance in C++
  • It is the process of deriving more than one class from a base class.
  • Inheritance is the process by which objects of one class acquire the properties of another class. 
  • By this we can add additional features to an existing class without modifying it.
  • This is possible by deriving a new class from the existing one. 
  • The new class will have combined features of both the classes.
  • Hierarchical Inheritance can be understand by following Syntax.

class A  

{  

    // body of the class A.  
}    
class B : public A   
{  
    // body of class B.  
}  
class C : public A  
{  
    // body of class C.  
}   
class D : public A  
{  
    // body of class D.  
 

Hierarchical Inheritance

//Program to implement Hierarchical Inheritance.
#include <iostream.h> 
#include <conio.h>

class A //single base class
{
    public:
  int x, y;
  void getdata()
  {
        cout << "\nEnter value of x and y:\n"; cin >> x >> y;
  }
};
class B : public A //B is derived from class base
{
    public:
  void product()
  {
      cout << "\nProduct= " << x * y;
  }
};
class C : public A //C is also derived from class base
{
    public:
  void sum()
  {
        cout << "\nSum= " << x + y;
  }
};
int main()
{
    B obj1;          //object of derived class B
    C obj2;          //object of derived class C
    obj1.getdata();
    obj1.product();
    obj2.getdata();
    obj2.sum();
    return 0;
}  

Expected Output:-

Enter value of x and y:
2
3
Product= 6
Enter value of x and y:
2
3
Sum= 5

//Let's see a simple example...

#include <iostream.h> 
#include <conio.h>
class Side
{
 protected:
 int l;
 public:
 void set_values (int x)
 { l=x;}
}; 
class Square: public Side
{
 public:
 int sq()
 { return (l*l); }
}; 
class Cube:public Side
{
 public:
 int cub()
 { return (l*l*l); }
}; 
int main ()
{
 Square s;
 s.set_values (10);
 cout << "The square value is=" << s.sq() << endl;
 Cube c;
 c.set_values (20);
 cout << "The cube value is=" << c.cub() << endl;
 return 0;
}
Expected Output:-
Total Objects: 1
The square value is= 100
The cube value is=8000

//Let's see a simple example...


#include <iostream.h> 

#include <conio.h> 

class Shape                 // Declaration of base class.  

{  
    public:  
    int a;  
    int b;  
    void get_data(int n,int m)  
    {  
        a= n;  
        b = m;  
    }  
};  
class Rectangle : public Shape  // inheriting Shape class  
{  
    public:  
    int rect_area()  
    {  
        int result = a*b;  
        return result;  
    }  
};  
class Triangle : public Shape    // inheriting Shape class  
{  
    public:  
    int triangle_area()  
    {  
        float result = 0.5*a*b;  
        return result;  
    }  
};  
int main()  
{  
    Rectangle r;  
    Triangle t;  
    int length,breadth,base,height;  
    std::cout << "Enter the length and breadth of a rectangle: " << std::endl;  
    cin>>length>>breadth;  
    r.get_data(length,breadth);  
    int m = r.rect_area();  
    std::cout << "Area of the rectangle is : " <<m<< std::endl;  
    std::cout << "Enter the base and height of the triangle: " << std::endl;  
    cin>>base>>height;  
    t.get_data(base,height);  
    float n = t.triangle_area();  
    std::cout <<"Area of the triangle is : "  << n<<std::endl;  
    return 0;  
}  
Expected Output:-

Enter the length and breadth of a rectangle:
23  
20  
Area of the rectangle is : 460          
Enter the base and height of the triangle:  
2   
5
Area of the triangle is : 5 

//Let's see a simple example...
   
#include <iostream.h> 
#include <conio.h>
  
//Base class 
class Parent 
    public: 
      int id_p; 
}; 
   
// Sub class inheriting from Base Class(Parent) 
class Child : public Parent 
    public: 
      int id_c; 
}; 
  
//main function 
int main()  
   { 
       
        Child obj1; 
           
        // An object of class child has all data members 
        // and member functions of class parent 
        obj1.id_c = 7; 
        obj1.id_p = 91; 
        cout << "Child id is " <<  obj1.id_c << endl; 
        cout << "Parent id is " <<  obj1.id_p << endl; 
          
        return 0; 
   }  
Expected Output:-

Child id is 7
Parent id is 91




Post a Comment

Previous Post Next Post