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

C++——从一个正方体类(Cube)和球体类(Sphere),派生出圆柱体类(Cylinder),设计成员函数,能够输出正方体、球体、圆柱体的面积和体积。

没注释的源代码

#include <iostream>

using namespace std;

class Cube

{

public:

    Cube(int a):a(a){}

    void Cube_area()

    {

        cout<<"正方体的面积:"<<a*a<<endl;

    }

    void Cube_tiji()

    {

        cout<<"正方体的体积:"<<a*a*a<<endl;

    }

protected:

    double a;

};

class Sphere

{

public:

    Sphere(int r):r(r){}

    void Sphere_area()

    {

        cout<<"球体的面积:"<<3.14*r*r*4<<endl;

    }

    void Sphere_tiji()

    {

        cout<<"球体的体积:"<<4/3*3.14*r*r*r<<endl;

    }

protected:

    double r;

};

class Cylinder:public Cube,public Sphere

{

public:

    Cylinder(int a,int r):Cube(a),Sphere(r){}

    void Cylinder_area()

    {

        cout<<"圆柱体的面积:"<<(3.14*r*r+a*a)*2<<endl;

    }

    void Cylinder_tiji()

    {

        cout<<"圆柱体的体积:"<<3.14*r*r*a<<endl;

    }

};

int main()

{

    Cube c1(3);

    c1.Cube_area();

    c1.Cube_tiji();

    Sphere s1(5);

    s1.Sphere_area();

    s1.Sphere_tiji();

    Cylinder c2(3,5);

    c2.Cylinder_area();

    c2.Cylinder_tiji();

    return 0;

}


http://www.mrgr.cn/news/57419.html

相关文章:

  • 6-4.Android 对话框之进度对话框问题清单(UI 线程问题、外部取消、dismiss 方法与 hide 方法)
  • 自动化数据库管理:如何通过存储过程动态创建 MySQL 对象
  • JMeter 动态参数赋值实践
  • 【Golang】Go语言http编程底层逻辑实现原理与实战
  • log4j2.xml
  • 使用 Spring 框架构建 MVC 应用程序:初学者教程
  • kconfig语法(二)
  • 数据结构 ——— C语言实现链式队列
  • 第五十四章 安全元素的详细信息 - DerivedKeyToken 详情
  • nginx负载均衡机制实现用户无感更新服务
  • 郑州市风景园林专项设计资质人员设置类别
  • 【蓝队技能】【Python规则开发】文件分析微步在线推送文件变化监控流量分析
  • 用Python实现批量解压所有ZIP文件
  • 债券的基础知识(一)
  • L1.【Leetcode笔记】删除有序数组中重复项
  • 从React Hooks看React的本质
  • mongoDB(初识(一)基本概念 ACID、 CAP、 BASE)
  • ECharts饼图-饼图34,附视频讲解与代码下载
  • C语言中#error的作用
  • STM32应用详解(7)USART接收数据的程序(查询方式)
  • 【力扣刷题实战】设计循环队列
  • 工作繁忙的同时,如何兼顾到CSDN的持续分享呢
  • Python中tkinter使用详解
  • Lucas带你机器学习实战——KNN预测未来的爆品
  • 递归算法之组合生成(Combinations)详细解读
  • 事务挂起的原因分析