#include <iostream>
using namespace std;
class Shape
{
public:
virtual float getArea() = 0;
};
class Circle : public Shape
{
public:
Circle(float r) { radius = r; }
void setRadius()
{
cout << "请输入圆的半径=" << endl;
cin >> radius;
}
float getArea()
{
return 3.14 * radius * radius;
}
private:
float radius;
};
class Rectangle : public Shape
{
public:
Rectangle(float l,float w) { length = l, width = w; }
void setLength()
{
cout << "请输入长方形的长=" << endl;
cin >> length;
cout << "请输入长方形的宽=" << endl;
cin >> width;
}
float getArea()
{
return length*width;
}
private:
float length;
float width;
};
class Tringle : public Shape
{
Tringle(float a, float b,float c);
public:
void setA()
{
cout << "请输入三角形的边长a=" << endl;
cin >> a;
cout << "请输入三角形的边长b=" << endl;
cin >> b;
cout << "请输入三角形的边长c=" << endl;
cin >> c;
}
float getArea()
{
return sqrt(0.5 * (a + b + c) * (0.5 * (a + b + c) - a) * (0.5 * (a + b + c) - b) * (0.5 * (a + b + c) - c));
}
private:
float a;
float b;
float c;
};
int main()
{
Shape* s[3]; // 定义一个指针数组
s[0] = new Circle();
s[1] = new Rectangle();
s[2] = new Tringle();
int i;
for (i = 0; i < 3; i++)
{
s[i]->getArea();
}
for (i = 0; i < 3; i++) // 用完后要释放堆上创建的对象
{
delete s[i];
}
return 0;
}
#不知道是什么问题,这怎么改
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- _GX_ 2021-12-31 04:22关注
帮你修改好了
#define _USE_MATH_DEFINES #include <iostream> #include <cmath> #include <stdexcept> class Shape { public: virtual ~Shape() {} virtual float getArea() const = 0; }; class Circle : public Shape { public: Circle(float radius) { setRadius(radius); } float getRadius() const { return _radius; } void setRadius(float radius) { if (radius <= 0.0f) throw std::invalid_argument("invalid circle"); _radius = radius; } float getArea() const { return M_PI * _radius * _radius; } private: float _radius; }; class Rectangle : public Shape { public: Rectangle(float length, float width) { setLength(length); setWidth(width); } float getLength() const { return _length; } void setLength(float length) { if (length <= 0.0f) throw std::invalid_argument("invalid rectangle"); _length = length; } float getWidth() const { return _width; } void setWidth(float width) { if (width <= 0.0f) throw std::invalid_argument("invalid rectangle"); _width = width; } float getArea() const { return _length * _width; } private: float _length; float _width; }; class Tringle : public Shape { public: Tringle(float a, float b, float c) { set(a, b, c); } float getA() const { return _a; } float getB() const { return _b; } float getC() const { return _c; } void set(float a, float b, float c) { if (a <= 0.0f || b <= 0.0f || c <= 0.0f || (a + b) <= c || (a + c) <= b || (b + c) <= a) throw std::invalid_argument("invalid triangle"); _a = a; _b = b; _c = c; } float getArea() const { float s = (_a + _b + _c) / 2.0f; return sqrt(s * (s - _a) * (s - _b) * (s - _c)); } private: float _a; float _b; float _c; }; int main() { try { Shape *s[3]; float r; std::cout << "请输入圆的半径="; std::cin >> r; s[0] = new Circle(r); float l, w; std::cout << "请输入长方形的长="; std::cin >> l; std::cout << "请输入长方形的宽="; std::cin >> w; s[1] = new Rectangle(l, w); float a, b, c; std::cout << "请输入三角形的边长a="; std::cin >> a; std::cout << "请输入三角形的边长b="; std::cin >> b; std::cout << "请输入三角形的边长c="; std::cin >> c; s[2] = new Tringle(a, b, c); for (int i = 0; i < 3; i++) std::cout << "图形" << i + 1 << "的面积=" << s[i]->getArea() << '\n'; for (int i = 0; i < 3; i++) delete s[i]; } catch (const std::exception &e) { std::cerr << e.what(); } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 公众号如何实现点击超链接后自动发送文字
- ¥15 用php隐藏类名和增加类名
- ¥15 算法设计与分析课程的提问
- ¥15 用MATLAB汇总拟合图
- ¥15 智能除草机器人方案设计
- ¥15 对接wps协作接口实现消息发送
- ¥15 SQLite 出现“Database is locked” 如何解决?
- ¥15 已经加了学校的隶属邮箱了,为什么还是进不去github education?😭
- ¥15 求会做聚类,TCN的朋友有偿线上指导。以下是目前遇到的问题
- ¥100 无网格伽辽金方法研究裂纹扩展的程序