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

Minimum shelf, At a shop of marbles, packs of marbles are prepared. Packets...

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

Write a simple telephone book program, Write a simple telephone book progra...

Write a simple telephone book program that stores the names and phone numbers of your friends/acquaintances in a file. Your program should have the abilities to: a. Add entries (n

What is token in programming languages, T o k e n :  Tokens  are  s...

T o k e n :  Tokens  are  small  entities  in  a  program.    Example: identifiers,  keywords,  constants, operators, strings, etc.  These tokens are used almost in same wa

String storage within c, The concept of a string in C is difficult, because...

The concept of a string in C is difficult, because a string is a collection of characters stored in memory terminated by a NULL string i.e. \0. Let us consider the string Hello. Th

Types of linked list with the help of diagrams, Problem: (a) Describe ...

Problem: (a) Describe a linked list. (b) Explain the three different types of linked list with the help of diagrams. (c) Give two advantages and two disadvantages o

Objects as function arguments, Objects as Function Arguments: In C prog...

Objects as Function Arguments: In C program there are several methods to define arguments, and in some case even a structure can be approved as an argument.  Similarly in C+

C++, #questAt a shop of marbles, packs of marbles are prepared. Packets are...

#questAt a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets wit

How can we simulate the concept of multiple inheritance, Problem: (a) U...

Problem: (a) Using a class hierarchy of your own choosing, with at least TWO subclass levels, show (i) the use of abstract and concrete forms of Java class and method in you

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