Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "iostream"
- #include "cmath"
- using namespace std;
- class Figure{
- protected:
- string TypeFigure;
- public:
- Figure(){}
- virtual ~Figure() {}
- string GetTypeFigure() { return TypeFigure; }
- virtual double GetArea() = 0;
- };
- class Triangle : public Figure{
- protected:
- double AB, BC, AC;
- public:
- Triangle(){}
- Triangle(double AB, double BC, double AC){
- this->AB = AB;
- this->BC = BC;
- this->AC = AC;
- TypeFigure = "Triangle";
- };
- double GetArea(){
- double p = (AB+BC+AC)/2;
- return sqrt(p*(p-AB)*(p-BC)*(p-AC));
- }
- };
- class Rectangle : public Figure{
- protected:
- double AB, BC;
- public:
- Rectangle(){}
- Rectangle(double AB, double BC){
- this->AB = AB;
- this->BC = BC;
- TypeFigure = "Rectangle";
- }
- double GetArea(){
- return AB*BC;
- }
- };
- class Circle : public Figure{
- protected:
- double R;
- public:
- Circle(){}
- Circle(double R): R(R) {TypeFigure = "Circle";}
- double GetArea(){
- return M_PI*R*R;
- }
- };
- int main(){
- string t;
- cout << "What type of figure?" << '\n';
- cin >> t;
- Figure *p;
- if (t == "Triangle"){
- double AB, BC, AC;
- cin >> AB >> BC >> AC;
- p = new Triangle(AB,BC,AC);
- }
- else if (t == "Rectangle"){
- double AB, BC;
- cin >> AB >> BC;
- p = new Rectangle(AB, BC);
- }
- else {
- int r;
- cin >> r;
- p = new Circle(r);
- }
- cout << "Area " << p->GetTypeFigure() << " = "<< p->GetArea();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement