定义抽象基类shape,由它派生出五个派生类:circle(圆形)、square(正方形)、rectangle( 长方形)、trapezoid (梯形)和triangle (三角形),用虚函数分别计算各种图形的面积,输出它们的面积和。要求用基类指针数组,每一个数组元素指向一个派生类的对象。pi=3.14159f,单精度浮点数计算。

时间: 2023-05-31 08:17:49 浏览: 272
### 回答1: 定义抽象基类Shape,其中包含一个纯虚函数计算面积。然后派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle(长方形)、Trapezoid(梯形)和Triangle(三角形),分别实现计算面积的虚函数。最后,使用基类指针数组,每个数组元素指向一个派生类的对象,并计算每个图形的面积并输出。 具体实现如下: ```c++ #include <iostream> using namespace std; const float pi = 3.14159f; class Shape { public: virtual float area() = ; // 纯虚函数,计算面积 }; class Circle : public Shape { private: float radius; public: Circle(float r) : radius(r) {} float area() { return pi * radius * radius; } }; class Square : public Shape { private: float side; public: Square(float s) : side(s) {} float area() { return side * side; } }; class Rectangle : public Shape { private: float length, width; public: Rectangle(float l, float w) : length(l), width(w) {} float area() { return length * width; } }; class Trapezoid : public Shape { private: float top, bottom, height; public: Trapezoid(float t, float b, float h) : top(t), bottom(b), height(h) {} float area() { return (top + bottom) * height / 2; } }; class Triangle : public Shape { private: float base, height; public: Triangle(float b, float h) : base(b), height(h) {} float area() { return base * height / 2; } }; int main() { Shape* shapes[5]; // 基类指针数组 shapes[] = new Circle(2.f); shapes[1] = new Square(3.f); shapes[2] = new Rectangle(2.f, 4.f); shapes[3] = new Trapezoid(2.f, 4.f, 3.f); shapes[4] = new Triangle(3.f, 4.f); float totalArea = .f; for (int i = ; i < 5; i++) { totalArea += shapes[i]->area(); cout << "Shape " << i + 1 << " area: " << shapes[i]->area() << endl; delete shapes[i]; // 释放内存 } cout << "Total area: " << totalArea << endl; return ; } ``` 输出结果: ``` Shape 1 area: 12.5664 Shape 2 area: 9 Shape 3 area: 8 Shape 4 area: 9 Shape 5 area: 6 Total area: 44.5664 ``` 其中,Shape 1 是圆形,半径为 2.,面积为 12.5664;Shape 2 是正方形,边长为 3.,面积为 9;Shape 3 是长方形,长为 4.,宽为 2.,面积为 8;Shape 4 是梯形,上底为 2.,下底为 4.,高为 3.,面积为 9;Shape 5 是三角形,底为 3.,高为 4.,面积为 6。最后,所有图形的面积之和为 44.5664。 ### 回答2: 抽象基类shape定义如下: ``` class shape { public: virtual float area() = 0; // 计算面积的虚函数 }; ``` 派生类circle、square、rectangle、trapezoid和triangle继承自抽象基类shape,各自实现area()函数: ``` class circle : public shape { public: circle(float r) : m_radius(r) {} virtual float area() { return m_radius * m_radius * pi; } private: float m_radius; }; class square : public shape { public: square(float s) : m_side(s) {} virtual float area() { return m_side * m_side; } private: float m_side; }; class rectangle : public shape { public: rectangle(float l, float w) : m_length(l), m_width(w) {} virtual float area() { return m_length * m_width; } private: float m_length; float m_width; }; class trapezoid : public shape { public: trapezoid(float a, float b, float h) : m_top(a), m_bottom(b), m_height(h) {} virtual float area() { return (m_top + m_bottom) * m_height / 2; } private: float m_top; float m_bottom; float m_height; }; class triangle : public shape { public: triangle(float b, float h) : m_base(b), m_height(h) {} virtual float area() { return m_base * m_height / 2; } private: float m_base; float m_height; }; ``` 使用基类指针数组,每一个数组元素指向一个派生类的对象: ``` shape* shapes[5]; shapes[0] = new circle(3.0f); shapes[1] = new square(2.0f); shapes[2] = new rectangle(3.0f, 4.0f); shapes[3] = new trapezoid(2.0f, 4.0f, 3.0f); shapes[4] = new triangle(5.0f, 2.0f); ``` 计算每个图形的面积并输出: ``` for (int i = 0; i < 5; i++) { std::cout << "shape " << i+1 << " area = " << shapes[i]->area() << std::endl; } ``` 其中,pi被定义为全局变量: ``` const float pi = 3.14159f; ``` 完整代码如下: ``` #include <iostream> const float pi = 3.14159f; class shape { public: virtual float area() = 0; // 计算面积的虚函数 }; class circle : public shape { public: circle(float r) : m_radius(r) {} virtual float area() { return m_radius * m_radius * pi; } private: float m_radius; }; class square : public shape { public: square(float s) : m_side(s) {} virtual float area() { return m_side * m_side; } private: float m_side; }; class rectangle : public shape { public: rectangle(float l, float w) : m_length(l), m_width(w) {} virtual float area() { return m_length * m_width; } private: float m_length; float m_width; }; class trapezoid : public shape { public: trapezoid(float a, float b, float h) : m_top(a), m_bottom(b), m_height(h) {} virtual float area() { return (m_top + m_bottom) * m_height / 2; } private: float m_top; float m_bottom; float m_height; }; class triangle : public shape { public: triangle(float b, float h) : m_base(b), m_height(h) {} virtual float area() { return m_base * m_height / 2; } private: float m_base; float m_height; }; int main() { shape* shapes[5]; shapes[0] = new circle(3.0f); shapes[1] = new square(2.0f); shapes[2] = new rectangle(3.0f, 4.0f); shapes[3] = new trapezoid(2.0f, 4.0f, 3.0f); shapes[4] = new triangle(5.0f, 2.0f); for (int i = 0; i < 5; i++) { std::cout << "shape " << i+1 << " area = " << shapes[i]->area() << std::endl; } return 0; } ``` ### 回答3: 回答: 抽象基类 shape 可以定义如下: ``` class shape { public: virtual float area() const = 0; }; ``` 其中,虚函数 `area` 返回该形状的面积,`const` 保证该函数不能修改对象内部状态。 接下来,定义五个派生类:circle、square、rectangle、trapezoid 和 triangle,它们都继承自 base 类,同时实现 `area` 方法: ``` class circle : public shape { private: float radius; public: circle(float r) : radius(r) {} float area() const override { return 3.14159f * radius * radius; } }; class square : public shape { private: float side; public: square(float s) : side(s) {} float area() const override { return side * side; } }; class rectangle : public shape { private: float width, height; public: rectangle(float w, float h) : width(w), height(h) {} float area() const override { return width * height; } }; class trapezoid : public shape { private: float top, bottom, height; public: trapezoid(float t, float b, float h) : top(t), bottom(b), height(h) {} float area() const override { return (top + bottom) * height / 2; } }; class triangle : public shape { private: float base, height; public: triangle(float b, float h) : base(b), height(h) {} float area() const override { return base * height / 2; } }; ``` 接下来,我们可以通过指针数组动态创建派生类的对象: ``` int main() { const int n = 5; shape *shapes[n] = { new circle(1.0f), new square(2.0f), new rectangle(2.0f, 3.0f), new trapezoid(1.0f, 2.0f, 3.0f), new triangle(2.0f, 3.0f) }; for (int i = 0; i < n; i++) { cout << "Area of shape " << (i+1) << " is " << shapes[i]->area() << endl; delete shapes[i]; } return 0; } ``` 在 main 函数中,通过指针数组创建了五个派生类的对象,并输出它们的面积。 需要注意的是,动态创建的对象需要在程序结束前销毁,这里使用 `delete` 来删除之前 new 出来的每个对象。

相关推荐

application/msword

最新推荐

recommend-type

按以下描述和要求建立两个类:基类 Rectangle(矩形类) 和派生类 Cube(正方体)

按以下描述和要求建立两个类:基类 Rectangle(矩形类) 和派生类 Cube(正方体) 1. Rectangle 私有成员:  double x1, y1; //左下角的坐标  double x2, y2; //右上角的坐标 公有成员:  带缺省值的构造...
recommend-type

新开0-1冷启动分行业指南.zip

新开0-1冷启动分行业指南.zip
recommend-type

FPGA课程实验代码-包含加法器.zip

FPGA课程实验代码-包含加法器.zip
recommend-type

胖东来运营管理-施工管理制度.pdf

胖东来运营管理-施工管理制度.pdf
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

从键盘输入一段英文字符串,其中包含多个字母‘h',请编写程序利用正则表达式,将英文字符串中的’h'全部改为‘H’

好的,我可以帮您解决这个问题。 您可以使用 Python 编写一个程序来实现这个功能。具体步骤如下: 1. 首先导入 re 模块,这个模块是 Python 中用于处理正则表达式的模块。 2. 然后定义一个包含英文字符串的变量,例如: text = "This is a string containing multiple h's." 3. 接下来,使用 re.sub() 方法来进行替换操作。re.sub() 方法的第一个参数是正则表达式,第二个参数是替换的字符或字符串,第三个参数是被替换的字符串。在这个例子里,我们需要将所有的小写字母 h 替换成大写字母 H,所以正则表达式可以写成
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依