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

Pebble merchent problem, There is a pebble merchant. He sells the pebbles, ...

There is a pebble merchant. He sells the pebbles, that are used for shining the floor. His main duty is to take the length of the room’s sides. But he sometimes mistakes doing that

#calculate percentge, #write a multilevel c++ program to take marks of 3 su...

#write a multilevel c++ program to take marks of 3 subjects and calculate percentage and display it

Single string programme, write a programme that allows a user to enter 5 di...

write a programme that allows a user to enter 5 distinct words and returns them as a single string

Wap to swap the three digit number, WAP TO SWAP THE THREE DIGIT NUMBER ...

WAP TO SWAP THE THREE DIGIT NUMBER void main () { int b,r,n,r1,r2; clrscr (); printf ("Enter the Value: "); scanf ("%d",&n); r=n%10; n=n/10; r1=n%10;

Explain hiding overloaded functions, Hiding overloaded functions We can...

Hiding overloaded functions We cannot overload a base class function by redefining it in a derived class with a dissimilar argument list. Consider examples to see if similar fu

Assign random integers to the variable, (Random Numbers) Write statements t...

(Random Numbers) Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤2 b) 1 ≤ n ≤100 c) 0 ≤ n ≤9 d) 1000 ≤ n ≤1112 e)

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

write a program to find the area under the curve y=f(x) between x=a & x=b

Example of function - c program, Here is a short program. It prints out the...

Here is a short program. It prints out the value of a variable "x". Ernie and Bert disagree about what will be printed: Ernie says, the value gets changed in "changeX" so it will p

Described the scope resolution operator?, A: It allows a program to referen...

A: It allows a program to reference an identifier in global scope which has been hidden by another identifier along with the same name in the local scope.

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