WRITE A PROGRAM FOR HIERARCHICAL INHERITANCE IN C++

 Que- Make a program for hierarchical inheritance?

#include<iostream>
using namespace std;

class a
{
protected:
    int num1, num2 ;
public:
  void getdata ()
  {
       cout<<"enter a number :";
       cin>>num1 ;
       cout<<"enter a number :";
       cin>>num2 ;
  }
  void display ()
  {
    num1=num1+num2;
    cout<<"sum of two numbers is: "<<num1<<endl<<"\n";
  }
};
class b: public a
{
 
};
class c: public a
{
 
};
int main()
{
    c ob;
    b ok;
   
    ob. getdata ();
    ob. display ();
    ok. getdata ();
    ok. display ();
    return 0;
}

Result:-



Post a Comment

Previous Post Next Post