| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 576 人关注过本帖
标题:体积输出问题
只看楼主 加入收藏
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:8 
体积输出问题
#include<iostream>
using namespace  std;
class  Box
{  public:
  Box(int,int,int);
  int volume();
  void  display();
private:
    int height;
    int width;
    int length;
};
Box::Box(int h,int w,int len):height(h),width(w),length(len){}

int Box::volume()
{return (height*width*length);}
void  Box::display()
{cout<<volume()<<endl;}
int main()
{ Box box1(15,20,30);
cout<<box1.volume<<endl;
cout<<box1.display<<endl;
return  0;
}
,长,宽,高我分别赋值15,20,30,为什么程序的输出结果是1呢??
搜索更多相关主题的帖子: private display public return volume 
2013-10-20 11:06
blueskiner
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:227
专家分:707
注 册:2008-9-22
收藏
得分:0 
函数调用要加括号的
cout<<box1.volume()<<endl;
cout<<box1.display()<<endl;
2013-10-20 11:10
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
收藏
得分:0 
回复 2楼 blueskiner
我在vc++里面运行的程序,加了()之后就报错,你试了吗
2013-10-20 13:23
blueskiner
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:227
专家分:707
注 册:2008-9-22
收藏
得分:4 
#include <iostream>

class Box
{
public:
    Box(int h, int w, int len);
    int volume();
    void display();
private:
    int height;
    int width;
    int length;
};
Box::Box(int h, int w, int len) : height(h), width(w), length(len)
{
}

int Box::volume()
{
    int tmp = height * width;
    return (tmp * length);
}
void Box::display()
{
    std::cout<<volume()<<std::endl;
}

int main(int argc, char** argv)
{
    Box box1(15, 20, 30);
    std::cout<<box1.volume()<<std::endl;
    box1.display();
    return 0;
}
2013-10-20 15:16
苑天尤
Rank: 2
等 级:论坛游民
帖 子:17
专家分:16
注 册:2013-10-12
收藏
得分:6 
你看这个程序符合你的要求不?
#include<iostream>
 using namespace  std;
 class  Box
 {
 public:
   Box(int,int,int);
   int volume();
   void  display();
 private:
     int height;
     int width;
     int length;
 };
 Box::Box(int h,int w,int len):height(h),width(w),length(len){}
 
int Box::volume()
 {
    return (height*width*length);
}
 void  Box::display()
 {
     cout<<volume()<<endl;
 }
 int main()
 {
     Box box1(15,20,30);
 cout<<box1.volume()<<endl;
 box1.display ();
 return  0;
 }
2013-10-20 17:00
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
收藏
得分:0 
回复 5楼 苑天尤
可以的,为什么我的程序就运行不出来呢,
2013-10-20 21:37
RockSonE
Rank: 1
等 级:新手上路
帖 子:2
专家分:7
注 册:2013-10-21
收藏
得分:0 
void  Box::display()
{cout<<volume()<<endl;}

cout<<box1.display<<endl;

看看这里,Box::display()函数是void型的吧?里面的内容本来就是输出了体积cout<<volume()<<endl;但又在主函数里面cout<<box1.display<<endl;,这就不行了吧?这样只会输出判断值,真为1,假为0.
2013-10-21 09:43
IT男year
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:82
专家分:106
注 册:2013-6-9
收藏
得分:0 
回复 7楼 RockSonE
对咯!大神就是大神!
2013-10-21 12:09
呜呜1
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2013-9-10
收藏
得分:0 
回复 7楼 RockSonE
非常感谢
2013-10-21 12:44
快速回复:体积输出问题
数据加载中...
 
   



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

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