| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 422 人关注过本帖
标题:有关继承问题,初学,求教
只看楼主 加入收藏
px200
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2013-4-16
结帖率:60%
收藏
已结贴  问题点数:10 回复次数:2 
有关继承问题,初学,求教
#include<iostream>
#include <math.h>
using namespace std;
class circle{
public:
    void surface(int r,float s);
private:
    int r;
    float s;
};
void circle::surface(int r,float s)
{s=3.141592*r*r;
 cout<<"圆 面积"<<" "<<"体积"<<endl;
 cout<<s<<" "<<"0"<<endl;
}
class globe:public circle{
public:
    void volume(int r1,float v1,float s1)
    {s1=4*3.141592*r1*r1;
     v1=4.0/3.0*3.141592*r1*r1*r1;
     cout<<"球 表面积"<<" "<<"体积"<<endl;
     cout<<s1<<" "<<v1<<endl;
    }

private:
    int r1;
    float v1;
    float s1;
};
class cylinder:public circle{
public:
    void volume(int r2,float v2,float s2,int h2)
    {s2=3.141592*r2*2*h2;
     v2=3.141592*r2*r2*h2;
     cout<<"圆柱 表面积"<<" "<<"体积"<<endl;
     cout<<s2<<" "<<v2<<endl;
    }

private:
    int r2;
    float v2;
    float d2;
    int h2;
};
class cone:public circle{
public:
    void volume(int r3,float v3,float s3,int h3)
    {s3=sqrt(r3*r3+h3*h3)*1.0/2.0*r3;
     v3=1.0/3.0*3.141592*r3*r3*r3;
     cout<<"圆锥 表面积"<<" "<<"体积"<<endl;
     cout<<s3<<" "<<v3<<endl;
    }
private:
    int r3;
    float v3;
    float s3;
    int h3;
};
int main()
{
    int r,r1,r2,r3;
    float s,s1,s2,s3;
    float v,v1,v2,v3;
    int h2,h3;
    circle cir;
    cout<<"请输入圆的半径"<<endl;
    cin>>r;
    globe glo;
    cout<<"请输入球的半径"<<endl;
    cin>>r1;
    cylinder cyl;
    cout<<"请输入圆柱的高和圆柱的底面半径"<<endl;
    cin>>r2>>h2;
    cone con;
    cout<<"请输入圆锥的高和圆锥的底面半径"<<endl;
    cin>>r3>>h3;
   
    cout<<cir.surface()<<endl;
    cout<<glo.volume()<<endl;
    cout<<cyl.volume()<<endl;
    cout<<con.volume()<<endl;
    return 0;
}



  我这继承应该不对吧,怎么改一下?下面是出现的错误,求解
ab-8.cpp
D:\VC++\program work\program work\lab8\lab-8.cpp(78) : error C2660: 'surface' : function does not take 0 parameters
D:\VC++\program work\program work\lab8\lab-8.cpp(79) : error C2660: 'volume' : function does not take 0 parameters
D:\VC++\program work\program work\lab8\lab-8.cpp(80) : error C2660: 'volume' : function does not take 0 parameters
D:\VC++\program work\program work\lab8\lab-8.cpp(81) : error C2660: 'volume' : function does not take 0 parameters
Error executing cl.exe.
搜索更多相关主题的帖子: private include public volume 
2013-05-16 17:47
Mr黑绵羊
Rank: 2
等 级:论坛游民
帖 子:24
专家分:30
注 册:2010-12-7
收藏
得分:10 
cout<<cir.surface()<<endl;
 cout<<glo.volume()<<endl;
没传参数。。。
楼主应该是想在构造函数里初始化半径和其他变量,然后调用surface(),volume()就可以输出结果吧。
那楼主要写个构造函数,接受特定参数并为变量赋值,surface(),volume()就可以不用形参了啊。
2013-05-16 18:26
px200
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2013-4-16
收藏
得分:0 
回复 2楼 Mr黑绵羊
不是的,我是想从键盘输入数据。通过继承来计算圆、球、圆柱和圆锥的表面积和体积。定义一个圆类,含半径成员。球类、圆柱类和圆锥类作为圆类的派生类。计算圆、球、圆柱和圆锥的表面积和体积。
2013-05-16 20:55
快速回复:有关继承问题,初学,求教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014875 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved