DYNAMIC POLYMORPHISM AND RTTI - Computer Programming

# Computer Programming

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

## Tuesday, October 26, 2010

PROGRAM:
#include<iostream.h>
#include<conio.h>
class Point
{
public:
int x; int 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<<"shape is drawm";
}
};
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 Rectangles : public Shape
{
Point leftbottom, lefttop, rightbottom, righttop;
public:
Rectangles(){}
Rectangles(Point tleftbottom, Point tlefttop, Point trightbottom, Point trighttop)
{
leftbottom = tleftbottom;
lefttop = tlefttop;
rightbottom = trightbottom;
righttop = trighttop;
}
void draw()
{
cout<<"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<<"Triangle is drawn at"<<avertex<<" "<<bvertex<<" "<<cvertex<<"\n";
}
};
class Circle: public Shape
{
public:
Circle(){}
{
center = tcenter;
}
void draw()
{
cout<<"Circle is drawn at"<<" " <<center<<" " <<"and the radius is: "<<radius<<"\n";
}
};
class Ellipses: public Shape
{
Point center;
public:
Ellipses(){}
Ellipses(Point tcenter, int tradius, int tangle)
{
center = tcenter;
angle = tangle;
}
void draw()
{
}
};
void main()
{
clrscr();
cout<<"\n";

Point p1(10,20);
Point p2(3,2);
Square sq(p1,5);
sq.draw();
Rectangles rect(p1,p2,p1,p2);
rect.draw();
Circle c(p1,50);
c.draw();
Ellipses e(p2,34,23);
e.draw();
Triangle t(p1,p2,p1);
t.draw();
Shape *s;
s=&sq;
s->draw();
s=&rect;
s->draw();
s=&t;
s->draw();
getch();
}