Dynamic Polymorphism: EX - NO:7 Date Program

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

EX.

NO:7 DYNAMIC POLYMORPHISM


DATE:7.9.10

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=&rect;
s->draw();
s=&t;
s->draw();
getch();
}

OUTPUT:

Square is drawn at(1020)and with length as5


Rectangle is drawn at ((1020),(1020))and((32),(32))

Circel is drawn at (1020) and the radius is 50

Triangle is drawn at(1020) (32) (1020)

You might also like