C program to display a rectangle, circle and triangle, C/C++ Programming

Aim: To implement a program to display a rectangle, circle and triangle.

Code:                      

class shape

{

            public:

                        virtual void dim()=0;

                        virtual void draw()=0;

};

class rect:public shape

{

            int top,bottom,left,right;

            public:

                        void dim()

                        {

                                    cout<<"Enter the dimensions of the rectangle\n";

                                    cout<<"Enter the left and top co-ordinate\n";

                                    cin>>left>>top;

                                    cout<<"Enter the right and bottom co-ordinate\n";

                                    cin>>right>>bottom;

                        }

                        void draw()

                        {

                                    rectangle(left,top,right,bottom);

                        }

};

class triangle:public shape

{

            int x1,x2,x3,y1,y2,y3;

            public:

                        void dim()

                        {

                                    cout<<"Enter the co-ordinates of the triangle\n";

                                    cout<<"Enter first co-ordinate\n";

                                    cin>>x1>>y1;

                                    cout<<"Enter second co-ordinate\n";

                                    cin>>x2>>y2;

                                    cout<<"Enter third co-ordinate\n";

                                    cin>>x3>>y3;

                        }

                        void draw()

                        {

                                    line(x1,y1,x2,y2);

                                    line(x2,y2,x3,y3);

                                    line(x3,y3,x1,y1);

                        }

};

class circ:public shape

{

            int x,y,r;

            public:

                        void dim()

                        {

                                    cout<<"Enter the centre of the circle\n";

                                    cin>>x>>y;

                                    cout<<"Enter the radius of the circle\n";

                                    cin>>r;

                        }

                        void draw()

                        {

                                    circle(x,y,r);

                        }

};

void main()

{

            int choice;

            int gd=DETECT,gm;

            shape *ptr;

            circ c;

            rect r;

            triangle t;

            clrscr();

            while(1)

            {

                        cout<<"Enter your choice\n";

                        cout<<"1. Draw rectangle\n2. Draw Circle\n3. Triangle\n4. Exit\n";

                        cin>>choice;

                        switch(choice)

                        {

                                    case 1:

                                                ptr=&r;

                                                ptr->dim();

                                                initgraph(&gd,&gm,"c:\\tc\\bgi");

                                                ptr->draw();

                                                getch();

                                                closegraph();

                                                break;

                                    case 2:

                                                ptr=&c;

                                                ptr->dim();

                                                initgraph(&gd,&gm,"c:\\tc\\bgi");

                                                ptr->draw();

                                                getch();

                                                closegraph();

                                                break;

                                    case 3:

                                                ptr=&t;

                                                ptr->dim();

                                                initgraph(&gd,&gm,"c:\\tc\\bgi");

                                                ptr->draw();

                                                getch();

                                                closegraph();

                                                break;

                                    case 4:

                                                exit(0);

                                    default:

                                                cout<<"You entered a wrong choice\n";

                        }

            }

}

Posted Date: 9/29/2012 4:15:46 AM | Location : United States







Related Discussions:- C program to display a rectangle, circle and triangle, Assignment Help, Ask Question on C program to display a rectangle, circle and triangle, Get Answer, Expert's Help, C program to display a rectangle, circle and triangle Discussions

Write discussion on C program to display a rectangle, circle and triangle
Your posts are moderated
Related Questions
Calculate the salary if the person work 8 hours/day and the salary/hour is RM5.20 - UML class diagaram -Class -Exception handling/assertion


Introduction. In this assignment you are required to revisit the Assignment 1 topic. You will develop a new program which is more sophisticated, in particular, adding more functio

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

The question description: You are required to make a C++ program to conduct timber component (beam and column) structural design according to EC5 (all the relevant design codes

a program to find the area under curve y=f(x) between x=a and x=b,integrate y=f(x) between the limits of a and b.

padovan string c++ program

You are working for a computer gaming company and have been asked to look at a survival game.  You will be simulating a rabbit stranded on an island, collecting a large number of s

Does improper inheritance have a potential to wreck a project? Many projects meet a dead end due to bad inheritance. Hence it certainly has potential to wreck a project. Sma

Write a C++ program according to the following specifications 1. Display a brief description of the program. 2. Ask the user to specify the type of loading (end load, intermediate