Wednesday 12 October 2011

DYNAMIC POLYMORPHISM AND RTTI


DYNAMIC POLYMORPHISM AND RTTI
AIM:
To write a C++ program to demonstrate a simple test application for dynamic polymorphism.

ALGORITHM:

Step 1: Start the program.
Step 2: Create a class called Point in order to locate the position of the shapes.
Step 3: Create a class called Shape and use virtual function to draw the corresponding shape.
Step 4: Create classes for shapes like Square, Rectangle, Triangle, Circle, Ellipses.
Step 5: Call all the shapes using objects, in main function.
Step 6: Shapes will be drawn with the given values.
Step 7: Shapes can also be drawn by using their address and display their location.
Step 8: Stop the program.

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();
}

OUTPUT:

Square is drawn at(1020)and with length as :5
Rectangle is drawn at ((1020), (1020))and((32),(32))
Circle is drawn at (1020) and the radius is: 50
Ellipse is drawn at(32)and the radius is34with an angle23
Triangle is drawn at(1020) (32) (1020)
Square is drawn at(1020)and with length as :5
Rectangle is drawn at ((1020), (1020))and((32),(32))
Triangle is drawn at(1020) (32) (1020)

RESULT:
Thus the C++ program to demonstrate a simple test application for dynamic polymorphism was executed.

0 comments:

Post a Comment