Professional Documents
Culture Documents
Dynamic Polymorphism: EX - NO:7 Date Program
Dynamic Polymorphism: EX - NO:7 Date Program
Dynamic Polymorphism: EX - NO:7 Date Program
PROGRAM:
#include<iostream.h>
#include<conio.h>
class point
{
public:
int x,y;
point()
{
}
point(int tempx,int tempy)
{
x=tempx;
y=tempy;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
friend ostream & operator<<(ostream &tempout,point &temppoint)
{
tempout<<"("<<temppoint.getx()<<temppoint.gety()<<")";
return tempout;
}
};
class shape
{
point position;
public:
shape()
{
}
virtual void draw()
{
cout<<"\n Shape is drawn";
}
};
class square:public shape
{
point leftbottom;
int length;
public:
square()
{
}
square(point tleftbottom, int tlength)
{
leftbottom=tleftbottom;
length=tlength;
}
void draw()
{
cout<<"Square is drawn at"<<leftbottom<<"and with length
as"<<length<<"\n";
}
};
class rectangle:public shape
{
point leftbottom,lefttop,rightbottom,righttop;
public:
rectangle()
{
}
rectangle(point tleftbottom, point tlefttop, point trightbottom, point
trighttop)
{
leftbottom=tleftbottom;
lefttop=tlefttop;
rightbottom=trightbottom;
righttop=trighttop;
}
void draw()
{
cout<<"\n Rectangle is drawn at
("<<leftbottom<<","<<rightbottom<<")"<<"and"<<"("<<lefttop<<","<<righttop<<")\n";
}
};
class triangle:public shape
{
point avertex,bvertex,cvertex;
public:
triangle()
{
}
triangle(point tavertex, point tbvertex, point tcvertex)
{
avertex=tavertex;
bvertex=tbvertex;
cvertex=tcvertex;
}
void draw()
{
cout<<"\n Triangle is drawn at"<<avertex<<" "<<bvertex<<" "<<"
"<<cvertex<<"\n";
}
};
class circle : public shape
{
point center;
int radius;
public:
circle()
{
}
circle(point tcenter, int tradius)
{
center=tcenter;
radius= tradius;
}
void draw()
{
cout<<"\nCircel is drawn at"<<" "<<center<<" "<<" and the radius
is "<<radius<<"\n";
}
};
void main()
{
clrscr();
point p1(10,20);
point p2(3,2);
square sq(p1,5);
rectangle rect(p1,p2,p1,p2);
circle c(p1,50);
triangle t(p1,p2,p1);
shape *s;
s=&sq;
s->draw();
s=▭
s->draw();
s=&t;
s->draw();
getch();
}
OUTPUT: