Example of constructor #include <iostream> using namespace std; class Time f public: Time() //the definition of constructor hour-0; minute=0; sec-0; void set_time(); void show time(); private: int hour; /private data members int minute; int sec; 2017年4月26日12时 第9章关于类和对象的进一步讨 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 7 #include <iostream> using namespace std; class Time {public: Time( ) //the definition of constructor { hour=0; minute=0; sec=0; }void set_time( ); void show_time( ); private: int hour; // private data members int minute; int sec; };
void Time:set time() /定义成员函数,向数据成员赋值 cin>>hour; cin>>minute; cin>>sec; void Time:show time() /定义成员函数,输出数据成员的值 {cout<<hour<<":"<<minute<<":"<<sec<<endl;} int mainO //The invoke of constructor { Time t1; /隐含调用构造函数 t1.show_time(); /∥显示t1的数据成员的值 t1.set time(); /对t1的数据成员赋值 t1.show_time(); /显示t1的数据成员的值 return 0; 2017年4月26日12时 第9章关于类和对象的进一步讨 8 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 8 void Time::set_time() //定义成员函数,向数据成员赋值 { cin>>hour; cin>>minute; cin>>sec; } void Time::show_time() //定义成员函数,输出数据成员的值 { cout<<hour<<":"<<minute<<":"<<sec<<endl;} int main() //The invoke of constructor { Time t1; //隐含调用构造函数 t1.show_time( ); //显示t1的数据成员的值 t1.set_time( ); //对t1的数据成员赋值 t1.show_time( ); //显示t1的数据成员的值 return 0; }
9.1.3 Constructor with parameters /求长方柱的体积 #include <iostream> using namespace std; class Box public: Box(int,int,int); //the declaration of constructor int volume(); private: int height; int width; int length; //define constructor outside of the class Box:Box(int h,int w,int len)//the realization of constructor { height=h; width-w; length=len; 2017年4月26日12时 第9章关于类和对象的进一步讨 9 H0务 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 9 //求长方柱的体积 #include <iostream> using namespace std; class Box {public: Box(int, int, int); //the declaration of constructor int volume( ); private: int height; int width; int length; };//define constructor outside of the class Box::Box(int h,int w,int len) //the realization of constructor { height=h; width=w; length=len; }
//Box::Box(int h,int w,int len):height(h),width(w), length(len){/用参数初始化表实现初始化,9.1.4 int Box:volumeO /定义计算体积的函数 { return(height*width*length); int main() Boxb0x1(12,25,30); /隐含调用构造函数,将初始值作为实参 cout<<"The volume of box1 is "<<box1.volume()<<endl; B0xb0x2(15,30,21); /隐含调用构造函数,将初始值作为实参 cout<<"The volume of box2 is "<<box2.volume()<<endl; return 0; 2017年4月26日12时 第9章关于类和对象的进一步讨 10 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 10 //Box::Box(int h,int w,int len):height(h),width(w), length(len){ }//用参数初始化表实现初始化,9.1.4 int Box::volume() //定义计算体积的函数 { return(height*width*length); } int main( ) { Box box1(12,25,30); //隐含调用构造函数,将初始值作为实参 cout<<"The volume of box1 is "<<box1.volume( )<<endl; Box box2(15,30,21); //隐含调用构造函数,将初始值作为实参 cout<<"The volume of box2 is "<<box2.volume( )<<endl; return 0; }
9.1.5 Overloading of constructors Similar to a common function,the constructors are also can be overloaded. The constructors which have similar function ,but different type and number of parameters,can be assigned the same function name. The overload constructors can be respectively recalled according to their parameters'type and number by the complier. 2017年4月26日12时 H0务 第9章关于类和对象的进一步讨 1 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 11 • Similar to a common function, the constructors are also can be overloaded. • The constructors which have similar function ,but different type and number of parameters, can be assigned the same function name. • The overload constructors can be respectively recalled according to their parameters’ type and number by the complier