1条回答 默认 最新
- 壹个小丑 2023-03-19 21:46关注
第二题代码如下:
#include <iostream> #include <cmath> using namespace std; class Shape { public: virtual double getArea() = 0; }; class Triangle : public Shape { private: double a, b, c; public: Triangle(double _a, double _b, double _c) : a(_a), b(_b), c(_c) {} double getArea() { double p = (a + b + c) / 2; return sqrt(p * (p - a) * (p - b) * (p - c)); } }; class Square : public Shape { private: double side; public: Square(double _side) : side(_side) {} double getArea() { return side * side; } }; class Circle : public Shape { private: double radius; public: Circle(double _radius) : radius(_radius) {} double getArea() { return M_PI * radius * radius; } }; int main() { Shape *s1 = new Triangle(3, 4, 5); Shape *s2 = new Square(2); Shape *s3 = new Circle(1); cout << "三角形面积:" << s1->getArea() << endl; cout << "正方形面积:" << s2->getArea() << endl; cout << "圆形面积:" << s3->getArea() << endl; delete s1; delete s2; delete s3; return 0; }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 oracle数据库查询语句问题
- ¥15 有没有c++绘制算法的佬们吗救孩一下
- ¥15 android 蓝牙闪退
- ¥15 绝缘子污秽comsol仿真参数
- ¥15 Fatal error in Process MEMORY
- ¥15 labelme生成的json有乱码?
- ¥30 arduino vector defined in discarded section `.text' of wiring.c.o (symbol from plugin)
- ¥20 如何训练大模型在复杂因素组成的系统中求得最优解
- ¥15 关于#r语言#的问题:在进行倾向性评分匹配时,使用“match it"包提示”错误于eval(family$initialize): y值必需满足0 <= y <= 1“请问在进行PSM时
- ¥45 求17位带符号原码乘法器verilog代码