Program for Inheritance in C++ | C++ Programming
Monday, March 02, 2020//program for inheritance
#include<iostream.h>
#include<conio.h>
class base
{ public:
int x;
int y;
base(int p,int a)
{ cout<<"\n Base class constructor invoked";
x=p;
y=a;
}
~base()
{ cout<<"\n Base class destructor invoked";
getch();
}
};
class derived:public base
{ public:
int d;
int e;
derived(int k,int l,int x,int y):base(x,y)
{ d=k;
e=l;
cout<<"\n Derived class constructor invoked";
cout<<"\n SUM="<<(d+e+x+y);
}
~derived()
{ cout<<"\n Derived class destructor invoked";
getch();
}
};
void main()
{ clrscr();
int f,g,h,i;
cout<<"\n Enter the value of first integer of base class:";
cin>>f;
cout<<"\n Enter the value of second integer of base class:";
cin>>g;
cout<<"\n Enter the value of first integer of derived class:";
cin>>h;
cout<<"\n Enter the value of second integer of derived class:";
cin>>i;
base obj1(f,g);
derived obj2(h,i,f,g);
getch();
}
#include<iostream.h>
#include<conio.h>
class base
{ public:
int x;
int y;
base(int p,int a)
{ cout<<"\n Base class constructor invoked";
x=p;
y=a;
}
~base()
{ cout<<"\n Base class destructor invoked";
getch();
}
};
class derived:public base
{ public:
int d;
int e;
derived(int k,int l,int x,int y):base(x,y)
{ d=k;
e=l;
cout<<"\n Derived class constructor invoked";
cout<<"\n SUM="<<(d+e+x+y);
}
~derived()
{ cout<<"\n Derived class destructor invoked";
getch();
}
};
void main()
{ clrscr();
int f,g,h,i;
cout<<"\n Enter the value of first integer of base class:";
cin>>f;
cout<<"\n Enter the value of second integer of base class:";
cin>>g;
cout<<"\n Enter the value of first integer of derived class:";
cin>>h;
cout<<"\n Enter the value of second integer of derived class:";
cin>>i;
base obj1(f,g);
derived obj2(h,i,f,g);
getch();
}
0 Comments