#include<iostream.h>
#include<conio.h>
class shape
{
protected:
int w,h,ans;
public:
shape()
{
w=h=0;
}
shape(int a, int b)
{
w=a;
h=b;
}
virtual void area()
{
ans=w*h;
}
virtual void display()
{
cout<<"\nArea of an shape is:"<<ans;
}
};
class rect:public shape
{
public:
rect(int c,int d):shape(c,d)
{
}
void area()
{
ans=w*h;
}
void display()
{
cout<<"\nArea of an rectangle is:"<<ans;
}
};
class square:public shape
{
public:
square(int c):shape(c,c)
{
}
void area()
{
ans=w*h;
}
void display()
{
cout<<"\nArea of an Square is:"<<ans;
}
};
void main()
{
shape *s;
rect r(5,2);
clrscr();
s=&r;
s->area();
s->display();
square sq(5);
s=&sq;
s->area();
s->display();
getch();
}
No comments:
Post a Comment