one_3 2021-10-29 14:21 采纳率: 33.3%
浏览 44
已结题

c++类得继承和派生 求求各位大佬解救新手小白

题目是要通过cylinder类继承height类和circle类计算圆柱体得面积和体积 但是我的运行结果是0.不知道问题出在哪里,感觉值没有传进去

#include <iostream>
#define pi 3.14
using namespace std;
class height
{
    protected:
    int h;
    public:
        height(int a)
        {
            h=a;
        }
    
        
 } ;
 class cirle
 {
     protected:
     int r;
     public:
         cirle(int b)
         {
             b=r;
         }
    
 };
 class cylinder :public height,public cirle
 {
     public:
         cylinder(int x,int y):height(h),cirle(r){
         }
         double area()
         {
             return (2*pi*r*r+2*pi*r*h);
         }
         double tiji()
         {
             return(pi*r*r*h);
         }
         void show()
         {
             cout<<"表面积:"<<area()<<endl<<"体积:"<<tiji();
         }
 };
 int main()
 {
     cylinder s(1,5);
     s.show();
     
 }

  • 写回答

1条回答 默认 最新

  • 关注

    第23行的 b=r; 改成 r = b;

    第30行的cylinder(int x,int y):height(h),cirle(r)应该是:
    cylinder(int x,int y):height(x),cirle(y)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月6日
  • 已采纳回答 10月29日
  • 创建了问题 10月29日

悬赏问题

  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义