Inheritance : Single level

#include <iostream.h>
#include <conio.h>
class Data
{
protected :
int num1;
int num2;
public :
void getData( int x, int y)
{
num1 = x;
num2 = y;
}
};
class Test : public Data
{
private :
int sum;
public :
void display( )
{
sum = num1 + num2;
cout<<”Num1 =”<<num1<<” Num2 =”<<num2<<”\n”;
cout<<”Sum =”<< sum<<”|n”;
}
};
void main( )
{
Test t1;
t1.getData(10,20 );
t1.display( );
}
Output :
Num1 = 10 Num2 = 20
Sum = 30

0 comments: