C++构造函数例子
#include<iostream>using namespace std;
//创建类
class Box
{
public:
Box(int h=8,int w=8,int l=8);
int volume();
private:
int height;
int width;
int length;
};
//定义构造函数
Box::Box(int h,int w,int l):height(h),width(w),length(l){}
//定义成员函数
int Box::volume()
{
return(height*width*length);
}
int main()
{
Box box1;
cout<<"体积是:"<<box1.volume()<<endl;
Box box3(18);
cout<<"体积是:"<<box3.volume()<<endl;
return 0;
}
页:
[1]