Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream.h>
- #include <conio.h>
- #include <typeinfo>
- class point
- {
- protected:
- int x,y;
- public:
- point()
- {
- x=0;
- y=0;
- }
- void getpoint()
- {
- cout<<"\nEnter the coordinates:";
- cin>>x;
- cin>>y;
- }
- void displaypoint()
- {
- cout<<"\nCoordinates are ("<<x<<","<<y<<")";
- }
- };
- class shape: public point
- {
- protected:
- float vertices,area,perimeter ;
- public:
- void getdetails()
- {
- getpoint();
- }
- void virtual get(){}
- void virtual fnarea(){}
- void virtual fnperimeter(){}
- void displayarea()
- {
- cout<<"\nArea="<<area;
- }
- void displayperi()
- {
- cout<<"\nPerimeter="<<perimeter;
- }
- };
- class square: public shape
- {
- float side;
- void get()
- {
- cout<<"\n the square object has been chosen randomly";
- cout<<"Enter the length of the side of the square: ";
- cin>>side;
- }
- void fnarea()
- {
- area=side*side;
- }
- void fnperimeter()
- {
- perimeter=4*side;
- }
- };
- class rectangle:public shape
- {
- float l,b;
- public:
- void get()
- {
- cout<<"\n the rectangle object has been chosen randomly";
- cout<<"\nEnter the length:";
- cin>>l;
- cout<<"\nEnter the breadth:";
- cin>>b;
- }
- void fnarea()
- {
- area=l*b;
- }
- void fnperimeter()
- {
- perimeter=2*(l+b);
- }
- };
- class circle:public shape
- {
- float radius;
- public:
- void get()
- {
- cout<<"\n the circle object has been chosen randomly";
- cout<<"\nEnter the Radius:";
- cin>>radius;
- }
- void fnarea()
- {
- area=3.14*radius*radius;
- }
- void fnperimeter()
- {
- perimeter=2*3.14*radius;
- }
- };
- class triangle: public shape
- {
- float base,height,b,c;
- public:
- void get()
- {
- cout<<"\n the triangle object has been chosen randomly";
- cout<<"\nEnter the base:";
- cin>>base;
- cout<<"\nEnter the height:";
- cin>>height;
- cout<<"\nEnter the other two sides:";
- cin>>b>>c;
- }
- void fnarea()
- {
- area=1/2*(base*height);
- }
- void fnperimeter()
- {
- perimeter=base+b+c;
- }
- };
- /*class ellipse:public shape
- {
- float majoraxis,minoraxis;
- public:
- void get()
- {
- cout<<"\nEnter the major axis";
- cin>>majoraxis;
- cout<<"\nEnter the minor axis";
- cin>>minoraxis;
- }
- void fnarea()
- {
- area=3.14*majoraxis*minoraxis;
- }
- void fnperimeter()
- {
- perimeter=(majoraxis*majoraxis+minoraxis*minoraxis)/2;
- }
- };
- class polygon:public shape
- {
- int no;
- float length;
- public:
- void get()
- {
- cout<<"\nEnter the no of vertices";
- cin>>no;
- cout<<"\nEnter the length of the side";
- cin>>length;
- }
- void fnperimeter()
- {
- perimeter=no*length;
- }
- };*/
- int main()
- {
- cout<<"\n A number will be chosen at random and as per the random digit the details of either a\n square, a rectangle, a triangle or a circle will be printed";
- int ch,loop,r;
- loop=0;
- shape *ptr;
- square sq;
- rectangle rect;
- triangle tri;
- circle cir;
- /*ellipse ell;
- polygon poly;*/
- while(loop!=1)
- {
- /*cout<<"\n1.Square\n2.Rectangle\n3.Circle\n4.Triangle\n5.Exit";
- cout<<"\nEnter your choice:";
- cin>>ch;*/
- ch=(rand()%7)+1;
- switch(ch)
- {
- case 1:
- ptr=&sq;
- break;
- case 2:
- ptr=▭
- break;
- case 3:
- ptr=○
- break;
- /*case 4:
- ptr=ℓ
- break;*/
- case 4:
- ptr=&tri;
- break;
- /*case 5:
- ptr=&poly;
- break;*/
- case 5:
- loop=1;
- break;
- /*default:
- cout<<"\nwrong choice";*/
- }
- /* if(typeid(*ptr)==typeid(poly))
- {
- ptr->getdetails();
- ptr->get();
- ptr->fnperimeter();
- ptr->displayperi();
- }
- else
- {*/
- ptr->getdetails();
- ptr->get();
- ptr->fnarea();
- ptr->fnperimeter();
- ptr->displayarea();
- ptr->displayperi();
- //}
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment