阅读背景:

带参数的构造函数及使用默认参数的构造函数

来源:互联网 

带参数的构造函数:

#include<iostream>
using namespace std;
class Box
{
   public:
      Box(int h=10,int w=10,int l=10);//使用有参的构造函数对类中数据初始化,即设定默认值
      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<<"the volume1 is "<<box1.volume()<<endl;
   Box box2(12);
   cout<<"the volume2 is "<<box2.volume()<<endl;
   Box box3(12,23);
   cout<<"the volume3 is "<<box3.volume()<<endl;
   Box box4(12,23,34);
   cout<<"the volume4 is "<<box4.volume()<<endl;
   system("pause");
   return 0;
}#include<iostream>
using namespa



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: