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

Change to palindrome, A palindrome is a string that reads the same from bot...

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

Exceptions handling in cpp, Exceptions. Your SVector class should throw fou...

Exceptions. Your SVector class should throw four exceptions: 3.1. If the constructor size is invalid, then it will just allow the implicit STL bad_alloc exception to pass up to mai

Overloading unary operators, Overloading Unary Operators class sign ...

Overloading Unary Operators class sign {int a,b,c; public: sign(){}; sign(int,int,int); void putdata(void); void operator-(); }; void sign::operator-() {a=

If statement, who to write max if statements in a program

who to write max if statements in a program

Compute the average of five numbers, Step 1 Define the program headers and ...

Step 1 Define the program headers and the variables      #include   #include   #include   #include void main()   {       char prompt;     float a,b,c,d,e;     floa

Vb.net, write a program that would accept the radius of the sphere and retu...

write a program that would accept the radius of the sphere and return its surface area.

Define passing by reference?, A: Method of passing arguments to a function ...

A: Method of passing arguments to a function that takes parameter of type reference.  for instance: void swap( int & x, int &amp;amp; y ) { int temp = x; x =

Loops, Create a program that will accept 3 numbers. The first number (num1)...

Create a program that will accept 3 numbers. The first number (num1) is the common difference and the second number (num2) is the starting number and the 3rd number (num3) is the m

Expression, i need expression and its types with example programs in c++

i need expression and its types with example programs in c++

Recursive function, Binomial coefficients are the numeric factors of the pr...

Binomial coefficients are the numeric factors of the products in a power of a binomial such as (x + y)n. For example, (x + y)2 = x2 + 2 x y + y2 has the coefficients 1 2 1. Binomia

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