Monday, June 7, 2010

graphics

//------------------------------------------------------------------------------------------------
//Aim:Write a prg to create a graphics class hierarchy.create abstract base class called
//figure and derive two classes close and open from that .decare two more classes
//called shape and ellipse using close class .create a derive clases polyline and line from open //cass
//(Run time polymorphism)
//------------------------------------------------------------------------------------------------

#include
#include
#include
class Figure            //abstract base class
    {
       public:        //all base class fun are virtual fun.
        virtual void draw()
         {
         }
        virtual void draw(int,int,int,int)
         {
         }
        virtual void draw(int,int,int)
         {
         }
        virtual void draw(int,int,int,int,int,int)
         {
         }
    };

class Close1 : public Figure             //deriving the close1 class from figure base class
    {
    public:
        void draw()           //draw fun with no argu.
         {
         }
        void draw(int,int,int) //draw fun with 3 argu.
         {
         }
        void draw(int,int,int,int)
         {
         }
        void draw(int,int,int,int,int,int)
         {
         }
    };

class Open : public Figure            //deriving the open class from figur base class
    {
    public:
        void draw()                 //multilines
         {
         }
        void draw(int,int,int,int)//to draw single line
         {
         }

    };
//deriving the shape calss from close1 base class
class shape : public Close1
    {
    public:
        void draw()                //fun to triangle
            {
              line(0, 0, 100,100);
              line(100, 100, 50, 150);
              line(50, 150, 0, 0);
            }
        void draw(int l,int t,int r,int b) //fun to draw rectangle
            {
            //rectangle(left,top,right,bottom);
               rectangle(l,t,r,b);
            }
        void draw(int x,int y,int r)       //fun to draw circle
            {
               circle(x,y,r);
            }

    };

//deriving the ellipse calss from close1 base class
class Ellipse : public Close1
    {
    void draw(int x,int y,int s,int e,int xrad,int yrad)//fun. to draw ellipse
        {
        //ellipse(midx, midy, stangle, endangle,xradius, yradius);
        ellipse(x, y, s, e,xrad, yrad);
        }
    };

class Line : public Open    //line class derived from open class
    {
    public:
    void draw(int x1,int y1,int x2, int y2)        //fun to draw line
        {
        /* draw a diagonal line*/
        line(x1, y1, x2, y2);
        }
    };

class Polyline : public Open     //polyline calss derived from open base class
    {
    public:
    void draw()         //fun. to draw polyline
        {
          line(20,150,100,100);
          line(150,140,20,150);
          line(250,50,50,150);
        }
    };

void main()
{
    Close1 *c[2];    //create pointer type array of object of close1 base class
    Open *o[2];    //create pointer type array of object of open base class
    shape p;     //create object of sahpe
    Polyline pl;    //create object of polyline
    Ellipse e;     //create a object of ellipse class
    Line l;     //create a object of line class
    int ch,x1,y1,r,x2,y2;
    clrscr();
    c[0]=&p;      //triangle rectangle circle
    c[1]=&e;     //ellipse
    o[0]=&pl;     //polyline
    o[1]=&l;      //line
    int gd = DETECT, gm;
    /* initialize graphics and local variables */
    initgraph(&gd, &gm, "d:\\tc\\bgi");

    do
    {
    cleardevice();
    cout<<"\nMENU";
    cout<<"\n1.Line\n2.Rectangle\n3.Ellipse\n4.Triangle\n5.PolyLine\n6.Circle\n7.Exit\n";
    cout<<"\nEnter ur Choice :";
    cin>>ch;
    switch(ch)
    {
    case 1:
        cout<<"\nEnter the coordinates(x1,y1,x2,y2) : ";
        cin>>x1>>y1>>x2>>y2;
        o[1]->draw(x1,y1,x2,y2);     //line
        break;
    case 2:
        cout<<"Enter the top(x1,y1) co-ordinates and bottom(x2,y2) :";
        cin>>x1>>y1>>x2>>y2;
        c[0]->draw(x1,y1,x2,y2);    //rectangle
        break;
    case 3:
        c[1]->draw(100,100,0,360,100,50);//ellipse
        break;
    case 4:
        c[0]->draw();            //triangle
        break;
    case 5:

        o[0]->draw();            //polyline
        break;
    case 6:
        cout<<"\nEnter the coordinates(x,y) and radius : ";
        cin>>x1>>y1>>r;
        c[0]->draw(x1,y1,r);        //circle
        break;
    }
    getch();
}while(ch!=7);                  //execute a loop till choice !=7;

}

No comments: