注册 登录
编程论坛 C++教室

请各位看看,谢谢

longyou2011 发布于 2011-01-30 18:27, 513 次点击
#include<iostream>
using namespace std ;
class Box
{
public :
    Box ()
    {
        length = 1 ; width = 1 ; height = 1 ;

    }
    Box ( float L , float w , float h )
    {
        length = L ; width = w ; height = h ;
    }
    float Surface { return ( length * width + length * height + width * height ) / 2 ; }
    float Volume { return ( length * width * height ) ; }
private :
    float length , width , height ;

} ;
int main ()
{
    Box box1 ( 1 , 2 , 2 ) ;
    Box box2 ( 1 , 2 , 2 ) ;
    cout << " Sourface of box1 is " << box1.Sourface << endl ;
    cout << " Volume of box2 is " << box2.Volume << endl ;
    return 0 ;
}错在了那里
4 回复
#2
zqmillet2011-01-30 20:02
错一:成员函数没形参也要打括号

float Surface { return ( length * width + length * height + width * height ) / 2 ; }
float Volume { return ( length * width * height ) ; }

改成

float Surface () { return ( length * width + length * height + width * height ) / 2 ; }
float Volume () { return ( length * width * height ) ; }


错二:调用成员函数的时候没形参也要打括号

cout << " Sourface of box1 is " << box1.Sourface << endl ;
cout << " Volume of box2 is " << box2.Volume << endl ;

改成

cout << " Sourface of box1 is " << box1.Sourface() << endl ;
cout << " Volume of box2 is " << box2.Volume() << endl ;


错三:单词和上次一样又拼错了

cout << " Sourface of box1 is " << box1.Sourface() << endl ;

改成

cout << " Sourface of box1 is " << box1.Surface() << endl ;


错四:表面积不是(ab+bc+ac)/2而是(ab+bc+ac)*2
逻辑错误是编译器没办法提示的,也是最难发现的,希望LZ认真

float Surface() { return ( length * width + length * height + width * height ) / 2 ; }

改成

float Surface() { return ( length * width + length * height + width * height ) * 2 ; }


除此之外没有问题,编译通过
只有本站会员才能查看附件,请 登录
#3
zqmillet2011-01-30 20:04
汗,所有的Surface都错了……
#4
longyou20112011-01-30 22:53
谢谢你的回答
#5
pangding2011-01-30 23:02
嗯,2 楼还挺认真的。值得大家学习~~
1