Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream> //pgm on class polygon
- using namespace std;
- class polygon
- {
- protected:
- float width, height;
- public:
- void input()
- {
- cout<<"\nEnter the height and width: ";
- cin>>height>>width;
- }
- virtual float area()=0;
- };
- class rectangle: public polygon
- {
- public:
- float area()
- {
- input();
- return width*height;
- }
- };
- class triangle:public polygon
- {
- public:
- //input();
- float area()
- {
- input();
- return (width*height)*0.5;
- }
- };
- int main()
- {
- polygon *p;
- char ch;
- float a;
- int opt;
- do
- {
- cout<<"\nEnter \n1 - area of rectangle\n2 - area of triangle\n";
- cin>>opt;
- switch(opt)
- {
- case 1:
- p=new rectangle;
- a=p->area();
- cout<<"\nArea of rectangle is : "<<a;
- break;
- case 2:
- p=new triangle;
- a=p->area();
- cout<<"\nArea of triangle is : "<<a;
- break;
- default:
- cout<<"\nInvalid option";
- }
- cout<<"\nDo you want to continue? [y/n] : ";
- cin>>ch;
- }while(ch=='y'|ch=='Y');
- delete p;
- }
Add Comment
Please, Sign In to add comment