![]() |
#2
wp2319572015-03-17 09:53
|

#include <iostream>
using namespace std;
class Box
{
public :
Box(int,int,int);
int volume( );
private :
int height;
int width;
int length;
};
//声明带参数的构造函数//声明计算体积的函数
Box::Box(int h,int w,int len) //在类外定义带参数的构造函数
这里的h、w、len有什么用,不太明白。对构造函数不太懂。已经有了height、width、length 为什么还要定义h、w、len呢
{
height=h;
width=w;
length=len;
}
int Box::volume( ) //定义计算体积的函数
{
return (height*width*length);这里为什么不是h、w、len呢,如果行14-19删去可不可以。
}
int main( )
{
Box box1(12,25,30); //建立对象box1,并指定box1长、宽、高的值
cout<<"The volume of box1 is "<<box1.volume( )<<endl;
Box box2(15,30,21); //建立对象box2,并指定box2长、宽、高的值
cout<<"The volume of box2 is "<<box2.volume( )<<endl;
return 0;
}
using namespace std;
class Box
{
public :
Box(int,int,int);
int volume( );
private :
int height;
int width;
int length;
};
//声明带参数的构造函数//声明计算体积的函数
Box::Box(int h,int w,int len) //在类外定义带参数的构造函数
这里的h、w、len有什么用,不太明白。对构造函数不太懂。已经有了height、width、length 为什么还要定义h、w、len呢
{
height=h;
width=w;
length=len;
}
int Box::volume( ) //定义计算体积的函数
{
return (height*width*length);这里为什么不是h、w、len呢,如果行14-19删去可不可以。
}
int main( )
{
Box box1(12,25,30); //建立对象box1,并指定box1长、宽、高的值
cout<<"The volume of box1 is "<<box1.volume( )<<endl;
Box box2(15,30,21); //建立对象box2,并指定box2长、宽、高的值
cout<<"The volume of box2 is "<<box2.volume( )<<endl;
return 0;
}
[ 本帖最后由 vvvevvv 于 2015-3-17 09:53 编辑 ]