Created
December 1, 2020 14:47
-
-
Save aditya-vijaykumar/1e168443a0f0cc18f4856f35b932aa43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Shape { | |
protected: double areaValue; | |
public: | |
virtual void area() = 0; | |
}; | |
class Triangle: public Shape { | |
protected: int height, base; | |
public: | |
Triangle(int h, int b){ | |
height = h; | |
base = b; | |
} | |
void area(){ | |
areaValue = 0.5*base*height; | |
cout<<"Calculating Triangle area."<<endl<<"Area = "<<areaValue<<" sq. units"<<endl<<endl; | |
} | |
}; | |
class Rectangle: public Shape { | |
protected: int length, breadth; | |
public: | |
Rectangle(int l, int b){ | |
length = l; | |
breadth = b; | |
} | |
void area(){ | |
areaValue = length*breadth; | |
cout<<"Calculating Rectangle area."<<endl<<"Area = "<<areaValue<<" sq. units"<<endl<<endl; | |
} | |
}; | |
class Circle: public Shape { | |
protected: int radius; | |
public: | |
Circle(int r){ | |
radius = r; | |
} | |
void area(){ | |
areaValue = 3.14*radius*radius; | |
cout<<"Calculating Circle area."<<endl<<"Area = "<<areaValue<<" sq. units"<<endl<<endl; | |
} | |
}; | |
int main() { | |
Shape *ptr; | |
Triangle ob1(4,3); | |
Rectangle ob2(3,6); | |
Circle ob3(5); | |
ptr = &ob1; | |
ptr->area(); | |
ptr = &ob2; | |
ptr->area(); | |
ptr = &ob3; | |
ptr->area(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment