# 求图形面积

triangleArea=50
rectangleArea=100
circleArea=314

#include <iostream>

using namespace std;

const double pi = 3.14;

class Shape {
public:
Shape(){}
Shape(double x,double y)
:m_x(x),m_y(y)
{}
virtual double Area(void) = 0;
virtual void OutPut(void) = 0;
protected:
double m_x;
double m_y;
};

class Triangle :public Shape {
public:
Triangle(double x,double y)
:Shape(x,y)
{}
double Area(void) {
return 0.5*m_x*m_y;
}
void OutPut(void) {
cout << "triangleArea=" << Area() << endl;
}
};

class Rectangle :public Shape {
public:
Rectangle(double x, double y)
:Shape(x, y)
{}
double Area(void) {
return m_x * m_y;
}
void OutPut(void) {
cout << "rectangleArea=" << Area() << endl;
}
};

class Circle :public Shape {
public:
Circle(double x)
:Shape(x,0)
{}
double Area(void) {
return pi * m_x*m_x;
}
void OutPut(void) {
cout << "circleArea=" << Area() << endl;
}
};

int main() {
double x, y;
cin >> x >> y;
Triangle t(x, y);
Rectangle r(x, y);
Circle c(x);
t.OutPut();
r.OutPut();
c.OutPut();
return 0;
}


©️2019 CSDN 皮肤主题: 创作都市 设计师: CSDN官方博客