Create an Abstract Base class called shape. Derive class rectangle from the base class shape and a class cube from the rectangle class. Data members : Length, width for class rectangle Height for class cube Member function : Area(), print() for class rectangle Volume(), print() for class cube Make function print() as virtual and declare as a pure virtual function in the base class. Write a main program to compute the area of rectangle and volume of cube and display the result using base class pointer.
#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void print()=0;
};
class rectangular:public shape
{
private:
float length,width,are;
public:
void area(int i,int j)
{
length=i;
width=j;
area=length*width;
}
void print()
{
cout<<"area is: "<<are<<"\n";
}
};
class cube :public rectangular
{
private:
float hight,vol;
public:
void volume(float h)
{
hight=h;
vol=hight*hight*hight;
}
void print()
{
cout<<"the volume is :" <<vol;
}
};
void main()
{
clrscr();
rectangular ob1;
cube ob2;
shape *sptr;
sptr=&ob1;
ob1.area(10,20);
sptr->print();
sptr=&ob2;
ob2.volume(25.5);
sptr->print();
getch();
}
No comments:
Post a Comment