| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 517 人关注过本帖
标题:C++编程出现点问题 求解答!!
只看楼主 加入收藏
YZAM
Rank: 1
等 级:新手上路
帖 子:8
专家分:5
注 册:2010-3-29
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:2 
C++编程出现点问题 求解答!!
建立类cylinder,包括两个数据成员radius和height,分别表示圆柱体的半径和高,cylinder类的构造函数被传递了两个double值来初始化这两个成员,定义成员函数area()和volume用来求圆柱体的表面积和体积,定义成员函数print()打印cylinder类的相关信息。最后函数中cylinder类的设计进行测试

我的编程如下 但无法让s,v在print()里输出 为什么 求解 !1谢谢
#include<iostream>
using namespace std;
const pi=3;
class cylinder{
   private:
       double s,radius;
       double v,height;
   public:
       cylinder(double r,double h)
       {radius=r;height=h;}
      void area()
       {double s;
       s=2*pi*radius*height;
       cout<<"s="<<s<<endl;
      }
      void volume()
       {double v;
       v=pi*radius*radius*height;
       cout<<"v="<<v<<endl;
     }
      void print()
       {cout<<"radius="<<radius<<endl;  // 为什么不能用print输出 s,v ?
       cout<<"height="<<height<<endl;}
};
    void main()
       {cylinder A(2.00,3.00);
    A.area();
    A.volume();
      A.print();
      
       }
搜索更多相关主题的帖子: 解答 
2010-04-05 13:03
书呆
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:55
专家分:188
注 册:2010-3-26
收藏
得分:5 
程序代码:
class cylinder
{
private:
    double radius;
    double height;
public:
    cylinder(double r,double h)
    {
        radius=r;
        height=h;
    }
   
    double area()
    {
        return (2*pi*radius*height);
    }
    double volume()
    {
        return (pi*radius*radius*height);
    }
    void print()
    {
        cout<<"radius="<<radius<<endl;
        cout<<"height="<<height<<endl;
        cout<<"s="<<area()<<endl;
        cout<<"v="<<volume()<<endl;
    }
};

沉醉东风月下读。柴门闭,莫管客来无。
2010-04-05 13:17
yyblackyy
Rank: 6Rank: 6
等 级:侠之大者
帖 子:98
专家分:457
注 册:2010-3-31
收藏
得分:5 
#include<iostream>
using namespace std;
const int pi=3;
class cylinder{
   private:
       double s,radius;
       double v,height;
   public:
       cylinder(double r,double h)
       {radius=r;height=h;}
      void area()
       {//double s; 省略,否则赋值的是局部变量s的值,不是类成员的s,那么你的print 就输出无意义的s值,这就是你说的输不出吧
       s=2*pi*radius*height+2*pi*radius*radius;//这才是表面积 ,你的是侧面积
       cout<<"s="<<s<<endl;
      }
      void volume()
       {//double v;  // 同上
       v=pi*radius*radius*height;
       cout<<"v="<<v<<endl;
     }
      void print()
       {
          cout<<"radius="<<radius<<endl;  // 为什么不能用print输出 s,v ?
       cout<<"height="<<height<<endl;
       cout<<"s="<<s<<endl;           //输出s
       cout<<"v="<<v<<endl;           //输出 v
      }
};
    void main()
       {cylinder A(2.00,3.00);
    A.area();
    A.volume();
      A.print();
      
       }
2010-04-05 13:58
快速回复:C++编程出现点问题 求解答!!
数据加载中...
 
   



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

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