c++ 编程  Define a class Circle and derive the class Cylinder from the class Circle.  The specific requirements are as follows:  In the main function, input the radius of the circle and output the area of the corresponding circle. Then, input the height of the cylinder, output the surface area and volume of the corresponding cylinder.

时间: 2024-02-20 20:01:59 浏览: 22
下面是一个符合您要求的 C++ 代码实现: ```cpp #include<iostream> using namespace std; class Circle { protected: double radius; public: Circle(double r) : radius(r) {} double getArea() const { return 3.14 * radius * radius; } }; class Cylinder : public Circle { private: double height; public: Cylinder(double r, double h) : Circle(r), height(h) {} double getSurfaceArea() const { return 2 * 3.14 * radius * height + 2 * Circle::getArea(); } double getVolume() const { return Circle::getArea() * height; } }; int main() { double r, h; cout << "Please input the radius of the circle: "; cin >> r; Circle c(r); cout << "The area of the corresponding circle is: " << c.getArea() << endl; cout << "Please input the height of the cylinder: "; cin >> h; Cylinder cy(r, h); cout << "The surface area of the corresponding cylinder is: " << cy.getSurfaceArea() << endl; cout << "The volume of the corresponding cylinder is: " << cy.getVolume() << endl; return 0; } ``` 在这个代码实现中,我们定义了一个圆形类 Circle,它有一个受保护的成员变量 radius 和一个公有函数 getArea(),返回圆形的面积。然后我们从 Circle 类派生出一个圆柱体类 Cylinder,它有一个私有成员变量 height 和两个公有函数 getSurfaceArea() 和 getVolume(),分别返回圆柱体的表面积和体积。在 main() 函数中,我们首先输入圆形的半径,创建一个 Circle 对象并输出其面积。然后输入圆柱体的高度,创建一个 Cylinder 对象并输出其表面积和体积。

相关推荐

最新推荐

recommend-type

使用c++编写和使用.so动态链接库

1,使用 c 生成动态链接库mylib.so的简单示例  声明文件mylib.h   #ifndef __MY_LIB_H__ #define __MY_LIB_H__ void foobar(int i);... #endif /* __MY_... printf("Printing from myli
recommend-type

require.js中的define函数详解

主要给大家介绍了关于require.js中define函数的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用require.js中的define函数具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
recommend-type

Python中对错误NameError: name ‘xxx’ is not defined进行总结

错误NameError: name ‘xxx’ is not defined总结情况一:要加双引号(” “)或者(’ ‘)而没加情况二:字符缩进格式的问题情况三:`if __name__==’__main__’ :` 没有和`class类`进行对齐情况四:NameError: ...
recommend-type

高质量C++、C编程指南.doc )

前 言 6 第1章 文件结构 11 1.1 版权和版本的声明 11 1.2 头文件的结构 12 1.3 定义文件的结构 13 1.4 头文件的作用 13 ...附录A :C++/C代码审查表 88 附录B :C++/C试题 93 附录C :C++/C试题的答案与评分标准 97
recommend-type

c++面试题基础分享.doc

c++面试题53个问题 1.C++的三大特性 2.C和C++的区别 3.全局变量和局部变量在内存分配上有何不同 4.static的作用 5.const解释其作用 6.指针和引用的区别 7.智能指针 8.简述深拷贝和浅拷贝的区别 9.编写my_...
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

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

spring添加xml配置文件

1. 创建一个新的Spring配置文件,例如"applicationContext.xml"。 2. 在文件头部添加XML命名空间和schema定义,如下所示: ``` <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
recommend-type

JSBSim Reference Manual

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