a1399774808 2015-11-12 06:11 采纳率: 100%
浏览 1301
已采纳

运行的时候结果不对,改了十多次还是不对,求各位大神解答

#include
#include
#include
using namespace std;
#define PI 3.1415
class circle
{
protected:
double R;
public:
circle(){R=0;}
circle(double r){R=r;}
virtual void display(){cout<<"请输入半径: "; cin>>R;}
};

class sphere:public circle
{
private:
double v1;
double s1;
public:
sphere(){}//球
sphere(double r):circle(r){}
void display(double R)
{
s1=4*PI*R*R;
v1=(PI*R*R*R*4)/3 ;
cout<<"球的表面积为:"<<s1<<endl<<"球的体积为:"<<v1<<endl;
}

};

class cylinder:public circle //圆柱
{
private:
double s2;
double v2;
double h;
public:
cylinder(){}
cylinder(double r,double H):circle(r){h=H;}
void display(double R)
{
cout<<"请输入圆柱的高:";
cin>>h;
s2=2*PI*R*R+2*PI*R*h;
v2=PI*R*R*h;
cout<<"圆柱的表面积为:"<<s2<<endl<<"圆柱的体积为:"<<v2<<endl;
}

};

class cone:public circle //圆锥
{
private:
double s3;
double v3;
double h;
public:
cone(){}
cone(double r,double H):circle(r){h=H;}
void display(double R)
{
cout<<"请输入圆锥的高:";
cin>>h;
s3=PI*R*R*sqrt(R*R+h*h)+PI*R*R;
v3=(PI*R*R*h)/3;
cout<<"圆锥的表面积为:"<<s3<<endl<<"圆锥的体积为:"<<v3<<endl;
}

};

void main()
{
cout<<"请选择:"< int l;
double R;
cin>>l;
circle pa;
pa.display();
if(l==1)
{
sphere a;
a.display(R);
}

else if(l==2) 
{ 
    cylinder b;
    b.display(R); 
} 
else if(l==3) 
{ 
    cone c; 
    c.display(R); 
} 

}

  • 写回答

2条回答 默认 最新

  • ysuwood 2015-11-12 09:54
    关注

    main函数中R使用前未初始化。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建