Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class shape
- {
- public:
- shape()
- {
- }
- virtual void compute()=0;
- };
- class triangle: public shape
- {
- public:
- double base,height,area;
- triangle()
- {
- }
- triangle(double b1,double h1):shape()
- {
- base=b1;
- height=h1;
- }
- void compute()
- {
- cout<<"Base of triangle: "<<base<<"\n";
- cout<<"Height of triangle: "<<height<<"\n\n";
- area=0.5*base*height;
- cout<<"Area of triangle: "<<area<<"\n";
- }
- };
- class rectangle:public shape
- {
- public:
- double length,width,ar;
- rectangle()
- {
- }
- rectangle(double l1,double w1):shape()
- {
- length=l1;
- width=w1;
- }
- void compute()
- {
- cout<<"length of rectangle: "<<length<<"\n";
- cout<<"width of rectangle: "<<width<<"\n\n";
- ar=length*width;
- cout<<"Area of rectangle: "<<ar<<"\n";
- }
- };
- int main()
- {
- double l,w,h,b;
- int i,x,p=1;
- shape *s;
- triangle t;
- rectangle r;
- while(p)
- {
- cout<<"\nEnter the operation you want to perform: ";
- cout<<"\n1. Area of triangle";
- cout<<"\n2. Area of rectangle";
- cout<<"\n3. Exit";
- cout<<"\n\n Enter your choice: ";
- cin>>x;
- switch(x)
- {
- case 1:
- cout<<"\n\nEnter the dimension of triangle";
- cout<<"\n\nEnter the height of the triangle: ";
- cin>>h;
- cout<<"\nEnter the base of the triangle: ";
- cin>>b;
- t=triangle(h,b);
- s=&t;
- s->compute();
- break;
- case 2:
- {
- cout<<"\n\nEnter the dimension of Rectangle";
- cout<<"\n\nEnter the length of the rectangle: ";
- cin>>l;
- cout<<"\nEnter the width of the rectangle: ";
- cin>>w;
- r=rectangle(l,w);
- s=&r;
- s->compute();
- break;
- }
- case 3:
- exit(0);
- break;
- }
- cout<<"\n\nDo you want to continue(1/0) ";
- cout<<"\nEnter your choice: ";
- cin>>p;
- }
- }
Add Comment
Please, Sign In to add comment