具体要求如下: ①定义平面二维点类CPoint,有数据成员x坐标,y坐标,函数成员(构造函数、纯虚函数求面积GetArea,纯虚函数求体积函数GetVolume、输出点信息函数print。 ②由CPoint类派生出圆类Cirle类(新增数据成员半径radius),函数成员(构造函数、求面积GetArea,虚函数求体积函数GetVolume、输出圆信息函数print。 ③再由Cirle类派生出圆柱体Ccylinder类(新增数据成员高度height),函数成员(构造函数、复制构造函数、求表面积GetArea,求体积函数GetVolume、输出圆柱体信息函数print。 ④在主函数测试这个这三个类。

时间: 2024-02-12 12:08:17 浏览: 40
好的,您的问题是如何定义一个平面二维点类CPoint并且从它派生出一个圆类Cirle和圆柱体类Ccylinder,实现相应的函数成员和数据成员,以及在主函数中进行测试。 首先,我们来定义CPoint类: ```cpp class CPoint { public: CPoint(double x = 0, double y = 0) : m_x(x), m_y(y) {} virtual ~CPoint() {} virtual double GetArea() const = 0; virtual double GetVolume() const = 0; virtual void print() const { std::cout << "Point(" << m_x << ", " << m_y << ")" << std::endl; } protected: double m_x; double m_y; }; ``` 在CPoint类中,我们定义了两个数据成员m_x和m_y,表示二维坐标系中点的x坐标和y坐标。同时,我们还定义了构造函数和纯虚函数GetArea()和GetVolume(),print()函数用于输出点的信息。 接下来,我们从CPoint类派生出一个圆类Cirle: ```cpp class Cirle : public CPoint { public: Cirle(double x = 0, double y = 0, double r = 0) : CPoint(x, y), m_radius(r) {} virtual ~Cirle() {} virtual double GetArea() const { return M_PI * m_radius * m_radius; } virtual double GetVolume() const { return 0; } virtual void print() const { std::cout << "Cirle(" << m_x << ", " << m_y << ", " << m_radius << ")" << std::endl; } protected: double m_radius; }; ``` 在Cirle类中,我们新增了一个数据成员m_radius,表示圆的半径。同时,我们重写了GetArea()函数,用于计算圆的面积,GetVolume()函数返回0,因为圆没有体积。print()函数用于输出圆的信息。 最后,我们从Cirle类派生出一个圆柱体类Ccylinder: ```cpp class Ccylinder : public Cirle { public: Ccylinder(double x = 0, double y = 0, double r = 0, double h = 0) : Cirle(x, y, r), m_height(h) {} Ccylinder(const Ccylinder& other) : Cirle(other), m_height(other.m_height) {} virtual ~Ccylinder() {} virtual double GetArea() const { return 2 * M_PI * m_radius * m_height + 2 * M_PI * m_radius * m_radius; } virtual double GetVolume() const { return M_PI * m_radius * m_radius * m_height; } virtual void print() const { std::cout << "Ccylinder(" << m_x << ", " << m_y << ", " << m_radius << ", " << m_height << ")" << std::endl; } protected: double m_height; }; ``` 在Ccylinder类中,我们新增了一个数据成员m_height,表示圆柱体的高度。我们重写了GetArea()和GetVolume()函数,用于计算圆柱体的表面积和体积。print()函数用于输出圆柱体的信息。 最后,在主函数中进行测试: ```cpp int main() { CPoint* p1 = new CPoint(1, 2); p1->print(); std::cout << "Area = " << p1->GetArea() << ", Volume = " << p1->GetVolume() << std::endl; Cirle* p2 = new Cirle(3, 4, 5); p2->print(); std::cout << "Area = " << p2->GetArea() << ", Volume = " << p2->GetVolume() << std::endl; Ccylinder* p3 = new Ccylinder(6, 7, 8, 9); p3->print(); std::cout << "Area = " << p3->GetArea() << ", Volume = " << p3->GetVolume() << std::endl; delete p1; delete p2; delete p3; return 0; } ``` 在主函数中,我们分别创建了一个CPoint对象、一个Cirle对象和一个Ccylinder对象,然后调用它们的print()函数、GetArea()函数和GetVolume()函数进行输出和计算。最后记得释放动态分配的内存。

相关推荐

请补全代码#include <iostream> #include <cstdlib> #include <cmath> #include <cstring> #include <ctime> using namespace std; /* 请编程实现一个简易的屏幕窗口模拟,具体要求如下: * 编写 CPoint 类,描述二维平面内的一个坐标点,包含 x,y 两个坐标,重载 >> 运算符以实现输出形如 (x,y) 的信息。 * 编写 CWindow 类,描述平面上的一个窗口,包含如下信息: int id ,窗口唯一标识,为了保证唯一性,可以使用 (int)time(NULL) 返回的时间戳赋值。 char title[255] ,窗口标题 CPoint topleft,bottomright ,左上角和右下角的坐标 两种形态的构造函数: 提供标题和两点坐标的:CWindow(const char* title,CPoint topleft,CPoint bottomright) 提供标题和左上角坐标以及窗口高宽的:CWindow(const char* title,CPoint topleft,int width,int height) 一个静态成员 CWindow* topmost ,指向当前活动窗口,可以初始化为 nullptr 一个 Activate() 方法,将当前窗口置为活动窗口 重载 >> 运算符输出窗口信息,形如 title (x,y)-(x,y) ,参见测试用例输出结果 * 其它成员如果需要请自行编写,例如 CPoint 可以设计 getX 和 getY 接口便于 CWindow 根据高宽计算右下角坐标 主程序生成了三个窗口并随机激活了一个,然后输出了激活后窗口的信息,请参考测试用例的输出进行编程。 / 点击在此输入一行或多行代码 //主程序 int main() { //主程序代码仅为测试类的设计而编写 //伪随机化,真随机请使用 srand(time(NULL)); //srand(2022); //最大窗口数量 const int MAX = 3; / 注意屏幕的坐标系为: (0,0) _____________x | | | y | */ //生成 MAX 个窗口对象存储在数组中 CWindow windows[MAX] {{"notepad",CPoint(10,20),CPoint(20,5)}, //提供两点坐标初始化 {"mspaint",CPoint(10,20),5,6}, //提供一点坐标加高宽初始化 {"code",CPoint(2,20),CPoint(30,1)} //提供两点坐标初始化 }; //随机选择一个窗口激活到前台 windows[rand()%MAX].Activate(); //输出这个前台窗口信息 cout << *CWindow::topmost << endl; return 0; }

最新推荐

recommend-type

Python_ChatGLM3 series Open Bilingual Chat LLMs 开源双语对话语言模型.zip

Python_ChatGLM3 series Open Bilingual Chat LLMs 开源双语对话语言模型
recommend-type

附件1:1名男大学生的一日食谱.xlsx

附件1:1名男大学生的一日食谱.xlsx
recommend-type

用“假定成功”思路做爆品.pdf

用“假定成功”思路做爆品
recommend-type

STC89C51单片机读取DS18B20的64位序列码并显示在1602液晶上KEIL工程文件+开发板硬件原理图.zip

STC89C51单片机读取DS18B20的64位序列码并显示在1602液晶上KEIL工程文件+开发板硬件原理图 /*读取DS18B20的64位序列码并显示在1602液晶上,如果读取正确结果,则在 液晶第一行显示DS18B20 OK,第二行显示序列码,如果读取失败,则在液晶上 显示DS18B20 ERR0R PLEASE CHECK ,用户可通过更改18B20接口自己外接。 */ #include <reg51.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int sbit DQ = P2^2; //定义DS18B20端口DQ sbit BEEP=P2^3 ; //蜂鸣器驱动线 bit presence ; sbit LCD_RS = P3^5; sbit LCD_RW = P3^6; sbit LCD_EN = P3^4; uchar code cdis1[ ] = {" DS18B20 OK "}; uchar code c
recommend-type

实验五 进程间通信.docx

实验五 进程间通信.docx
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

MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略

![MySQL数据库性能提升秘籍:揭秘性能下降幕后真凶及解决策略](http://xiaoyuge.work/explain-sql/index/2.png) # 1. MySQL数据库性能优化概述** MySQL数据库性能优化是一项至关重要的任务,可以显著提高应用程序的响应时间和整体用户体验。优化涉及识别和解决影响数据库性能的因素,包括硬件资源瓶颈、软件配置不当和数据库设计缺陷。通过采取适当的优化策略,可以显著提升数据库性能,满足业务需求并提高用户满意度。 # 2. MySQL数据库性能下降的幕后真凶 ### 2.1 硬件资源瓶颈 #### 2.1.1 CPU利用率过高 **症状:
recommend-type

如何在unity创建按钮

在 Unity 中创建按钮的步骤如下: 1. 在 Unity 中创建一个 UI Canvas,选择 GameObject -> UI -> Canvas。 2. 在 Canvas 中创建一个按钮,选择 GameObject -> UI -> Button。 3. 在场景视图中调整按钮的位置和大小。 4. 在 Inspector 中设置按钮的文本、颜色、字体等属性。 5. 添加按钮的响应事件,选择按钮,在 Inspector 的 On Click () 中添加相应的方法。 这样就可以创建一个按钮了,你可以在游戏中使用它来触发相应的操作。
recommend-type

JSBSim Reference Manual

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