Google Search

Tuesday, October 26, 2010

DYNAMIC POLYMORPHISM AND RTTI


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
{
 Point center; int radius;
 public:
 Circle(){}
 Circle(Point tcenter, int tradius)
 {
  center = tcenter;
  radius = tradius;
 }
 void draw()
 {
  cout<<"Circle is drawn at"<<" " <<center<<" " <<"and the radius is: "<<radius<<"\n";
 }
};
class Ellipses: public Shape
{
 Point center;
 int radius; int angle;
 public:
 Ellipses(){}
 Ellipses(Point tcenter, int tradius, int tangle)
 {
  center = tcenter;
  radius = tradius;
  angle = tangle;
 }
 void draw()
 {
  cout<<"Ellipse is drawn at"<<center<<"and the radius is"<<radius<<"with an angle"<<angle<<"\n";
 }
};
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();
}

0 comments:

Post a Comment