INHERITANCE


AIM:
To Write a program to implement the inheritance concept.



ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables in base class (sub class).
STEP 3: Derive the class from base class and declare the variables.
STEP 4: Create object for derived class and get the value of x and y.
STEP 5: Stop the program Execution.



PROGRAM:
#include<iostream.h>
#include<conio.h>
class Base
{
public:
int x;
void set_x(int n)
{
x=n;
}
void show_x()
{
cout<<"\n\t Base class...";
cout<<"\n\t x="<<x;
}
};
class derived:public Base
{
int y;
public:
void set_y(int n)
{
y=n;
}
void show_xy()
{
cout<<"\n\n\t Derived class...";




WWW.VIDYARTHIPLUS.COM


V.                   TEAM


 WWW.VIDYARTHIPLUS.COM


cout<<"\n\t x="<<x; cout<<"\n\t y="<<y;
}
};
void main()
{
clrscr();
derived obj;
int x,y;
cout<<"\n Enter the value of x";
cin>>x;
cout<<"\n Enter the value of y";
cin>>y;
obj.set_x(x); obj.set_y(y);
obj.show_x();
obj.show_xy();
getch();

}

OUTPUT: 
Enter the value of x 2 
Enter the value of y 3 
Base class... 
x=2 
Derived class... 
x=2 
y=3