例9.3构造函数的重载 #include <iostream> using namespace std; class Box { public: B0x(); /声明一个无参的构造函数 Box(int h,int w,int len):height(h),width(w),length(len) 声明一个有参的构造函数,用参数的初始化表对数据成员初始化 int volume(); private: int height; int width; int length; Box:BoxO) 定义一个无参的构造函数 { height=10; width=10; length=10; 2017年4月26日12时 H0务 第9章关于类和对象的进一步讨 12 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 12 #include <iostream> using namespace std; class Box { public: Box( ); //声明一个无参的构造函数 Box(int h,int w,int len):height(h),width(w),length(len){ } //声明一个有参的构造函数,用参数的初始化表对数据成员初始化 int volume( ); private: int height; int width; int length; }; Box::Box() //定义一个无参的构造函数 { height=10; width=10; length=10; }
int Box:volume() return(height*width*length); int main() Box box1; /建立对象box1,不指定实参 cout<<"The volume of box1 is "<<box1.volume()<<endl; B0xb0x2(15,30,25); /建立对象b0x2,指定3个实参 cout<<"The volume of box2 is "<<box2.volume()<<endl; return 0; 2017年4月26日12时 第9章关于类和对象的进一步讨 13 H0务 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 13 int Box::volume( ) { return(height*width*length); } int main( ) { Box box1; //建立对象box1,不指定实参 cout<<"The volume of box1 is "<<box1.volume( )<<endl; Box box2(15,30,25); //建立对象box2,指定3个实参 cout<<"The volume of box2 is "<<box2.volume( )<<endl; return 0; }
说明: (I)无参的构造函数称为默认构造函数(default constructor)。一个类只能有一个默认构造函数。 2)如果在建立对象时选用无参构造函数,应注意定 义对象的语法 Box box1; ∥Box box1O则是错误的; (3)在一个类中可以包含多个构造函数,但建立对象 时只执行其中一个构造函数。 2017年4月26日12时 H0座务 第9章关于类和对象的进一步讨 14 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 14 (1) 无参的构造函数称为默认构造函数(default constructor)。一个类只能有一个默认构造函数。 (2) 如果在建立对象时选用无参构造函数,应注意定 义对象的语法。 Box box1; // Box box1()则是错误的; (3) 在一个类中可以包含多个构造函数,但建立对象 时只执行其中一个构造函数
9.1.6 Constructor with default parameters As to the constructor with default parameters, you should transfer parameters to constructors when defining the object,or the constructor can't be executed.But in reality, there are several constructors whose parameters are usually invariable 2017年4月26日12时 H0身 第9章关于类和对象的进一步讨 15 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 15 • As to the constructor with default parameters, you should transfer parameters to constructors when defining the object, or the constructor can’t be executed. But in reality, there are several constructors whose parameters are usually invariable
9.1.6使用默认参数的构造函数 #include <iostream> using namespace std; class Box { public: Box(int=10,int=10,int=10);/形参有默认值,形参名可以省略 int volume(); private: int height; int width; int length; }; Box:Box(int h,int w,int len) /在定义函数时可以不指定默认参数 height-h; width-w; length=len; //Box:Box(int h,int w,int len):height(h),width(w),length(len) 2017年4月26日12时 H0务 第9章关于类和对象的进一步讨 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 16 #include <iostream> using namespace std; class Box { public: Box(int =10,int =10,int =10); //形参有默认值,形参名可以省略 int volume( ); private: int height; int width; int length; }; Box::Box(int h,int w,int len) //在定义函数时可以不指定默认参数 { height=h; width=w; length=len; }//Box::Box(int h,int w,int len):height(h),width(w),length(len){ }