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

Assignment Help:

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";

                        }

            }

}


Related Discussions:- C program to display a rectangle, circle and triangle

Find the largest and smallest average, Use the above problem and have ...

Use the above problem and have the program print the biggest and smallest sales for each employee for everyday of the month. Program should also find the largest and

Explain compound assignment operators, Compound Assignment Operators Ap...

Compound Assignment Operators Apart from the binary and the unary arithmetic operators, we also have compound assignment operators. These are +=, -=, *=, /=, %=. Using these op

Program decision making instructions, Within programs we very often want to...

Within programs we very often want to execute a different section of code, depending upon various conditions within C the If statement is used to achieve this. The If statement con

Explain static class members, Static Class Members As we already know a...

Static Class Members As we already know all the objects of the class have dissimilar data members but invoke the similar member functions. Though, there is an exception to this

C program to show overloading of matrix operator, C program to show overloa...

C program to show overloading of matrix operator: Write a program for matrix operator overloading. class matrix{                   private :                 int x[

Vectors, A body which has three forces acting on it is in equilibrium. One ...

A body which has three forces acting on it is in equilibrium. One force is 3N to the North and the other is 4N to the west. What us the magnitude and direction of the third force?

Implementing Dynamic Binding for RPC, i didnt no how to write and how to im...

i didnt no how to write and how to implement and the programming can be done in C and execution should be done in UNIX

What are the different steps in executing a c program, Question 1 What are...

Question 1 What are the different steps in executing a C program? Explain Question 2 What are the commonly used input/output functions in C? How are they accessed?

What does it mean to declare a function as a static, (c) A static member fu...

(c) A static member function can access static member data only, static member functions and functions and data outside the class. A static member function may be called, even whil

Area under curve, write a program to find the area under the curve y=f(x) b...

write a program to find the area under the curve y=f(x) between x=a and x=b integrate y=f(x) between the limits of a and b #include float start_point,

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd