构造函数

来源:互联网 发布:我的世界mac版在哪买 编辑:程序博客网 时间:2024/05/21 11:21

#include<iostream>
using namespace std;
class Box
{
public:
 Box();
 Box(int l,int w,int h):length(h),width(w),height(h){};

 int volume();
private:
 int length;
 int width;
 int height;
};

Box::Box()
{
 length=10;
 width=10;
 height=10;
}

int Box::volume()
{return(length * width * height);}

int main()
{
 Box box1;
 cout<<box1.volume()<<endl;
 Box box2(15,30,25);
 cout<<box2.volume()<<endl;
 return 0;
}

原创粉丝点击