Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- class Circle;
- class Shape
- {
- public:
- virtual double getArea()=0;
- virtual void Display()=0;
- };
- class Circle:public Shape
- {
- double radius;
- public:
- Circle(double r)
- {
- radius=r;
- }
- double getArea()
- {
- return 3.1416*radius*radius;
- }
- void Display()
- {
- cout<<getArea()<<endl;
- }
- };
- class Triangle:public Shape
- {
- double height;
- double ground;
- public:
- Triangle(double h,double g)
- {
- height=h;
- ground=g;
- }
- double getArea()
- {
- return 0.5*height*ground;
- }
- void Display()
- {
- cout<<getArea()<<endl;
- }
- };
- int main()
- {
- int n;
- double a,h,g;
- // Shape s;
- //Circle o(3);
- // Triangle
- Shape *ptr;
- cout<<"Which area of shape you wanna calculate?"<<endl;
- cout<<"1. "<<"Circle"<<endl;
- cout<<"2. "<<"Triangle"<<endl;
- cin>>n;
- if(n==1)
- {
- cout<<"Enter radius of Circle:";
- cin>>a;
- Circle o(a);
- ptr = &o;
- ptr->getArea();
- cout<<"Area is:";
- ptr->Display();
- }
- else if(n==2)
- {
- cout<<"Enter height and ground:";
- cin>>h;
- cin>>g;
- Triangle t(h,g);
- ptr = &t;
- ptr->getArea();
- ptr->Display();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement