当前位置: 首页 > news >正文

【问题描述】编写一个程序计算出球、圆柱和圆锥的表面积和体积。

【问题描述】

编写一个程序计算出球、圆柱和圆锥的表面积和体积。

要求:

(1)定义一个基类,至少含有一个数据成员半径,并设为保护成员;

(2)定义基类的派生类球、圆柱、圆锥,都含有求表面积和体积的成员函数和输出函数;

(3)编写主函数,求球、圆柱、圆锥的表面积和体积。

注:圆周率取3.14 

const double PI=3.14;

【输入形式】

程序参考的输入(数字前面为提示文字):

Input the radius of the sphere:30

Input the radius and height of the cylinder:30 40

Input the radius and height of the cone:30 40

【输出形式】

程序参考的输出:

The area of the sphere:11304

The volume of the sphere:113040

The area of the cylinder:13188

The volume of the cylinder:113040

The area of the cone:7536

The volume of the cone:37680

#include  <string>
#include  <cmath>
using  namespace  std;
const double PI = 3.14;class radius
{
protected:double r;
};class sphere: public radius
{
public:sphere(){cout<<"Input the radius of the sphere:";cin>>r;}void area(){cout<<"The area of the sphere:"<<4*PI*r*r<<endl;}void volume(){cout<<"The volume of the sphere:"<<(4*PI*r*r*r)/3<<endl;}
};class cylinder: public radius
{
public:cylinder(){cout<<"Input the radius and height of the cylinder:";cin>>r>>h;}void area(){cout<<"The area of the cylinder:"<<2*PI*r*(r+h)<<endl;}void volume(){cout<<"The volume of the cylinder:"<<PI*r*r*h<<endl;}
private:double h;
};class cone: public radius
{
public:cone(){cout<<"Input the radius and height of the cone:";cin>>r>>h;}void area(){cout<<"The area of the cone:"<<PI*r*sqrt(r*r+h*h)+PI*r*r<<endl;}void volume(){cout<<"The volume of the cone:"<<(PI*r*r*h)/3<<endl;}
private:double h;
};int  main()
{sphere a;cylinder b;cone c;a.area();a.volume();b.area();b.volume();c.area();c.volume();return  0;
}

http://www.lryc.cn/news/60521.html

相关文章:

  • Python 人工智能:16~20
  • 【华为OD机试真题】最优资源分配(javapython)
  • git的使用——操作流程
  • Ae:自动定向
  • ClickHouse入门详解
  • javaweb笔记2
  • 【IIS搭建网站】本地电脑做服务器搭建web站点并公网访问「内网穿透」
  • 算法训练day2:哈希表
  • Git——利用SSH密钥本地仓库上传远程GitHub库
  • 一起读源码 —— Fastjson 的核心方法及其实现原理
  • Python实现批量图片下载及去重处理
  • 【QA】Python代码调试之解决Segmentation fault (core dumped)问题
  • C++ 迭代器之旅(Journey of Iterators)
  • 使用全球融合CDN的10大优势
  • 前端学习:HTML图像、表格、列表
  • 202305读书笔记|《因思念而沉着》——任何赞美都是身外之物唯自由可随身携带
  • M1 M2上能安装上Autocad 2024 Mac 中文版吗 autocad m1 m2版本有啦 终于支持Ventura 13x了
  • 【题解】P4055 [JSOI2009] 游戏
  • P1020 [NOIP1999 普及组] 导弹拦截
  • Makefile学习
  • 2.4 随机变量函数的分布
  • 数据结构【一】:前缀表达式与后缀表达式的区别
  • 搭建 PostgreSQL
  • Nmap入门到高级【第四章】
  • c++正则表达式及其使用,超级详细
  • 【LeetCode: 剑指 Offer II 099. 最小路径之和 | 暴力递归 | DFS =>记忆化搜索=>动态规划】
  • Python OpenCV 计算机视觉:6~7
  • LabView中数组的使用2-1
  • Android 10.0 系统systemui下拉通知栏的通知布局相关源码分析
  • 研读Rust圣经解析——Rust learn-3(变量与可变性,数据类型)