int Box:volume() return(height*width*length); int main() { Box box1; /没有给实参 cout<<"The volume of box1 is "<<box1.volume()<<endl; Box box2(15); ∥只给定一个实参 cout<<"The volume of box2 is "<<box2.volume()<<endl; B0xb0x3(15,30); ∥只给定2个实参 cout<<"The volume of box3 is "<<box3.volume()<<endl; B0xb0x4(15,30,20); /给定3个实参 cout<<"The volume of box4 is "<<box4.volume()<<endl; return 0; 2017年4月26日12时 H0务 第9章关于类和对象的进一步讨 17 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 17 int Box::volume( ) { return(height*width*length);} int main( ) { Box box1; //没有给实参 cout<<"The volume of box1 is "<<box1.volume( )<<endl; Box box2(15); //只给定一个实参 cout<<"The volume of box2 is "<<box2.volume( )<<endl; Box box3(15,30); //只给定2个实参 cout<<"The volume of box3 is "<<box3.volume( )<<endl; Box box4(15,30,20); //给定3个实参 cout<<"The volume of box4 is "<<box4.volume( )<<endl; return 0; }
Note: When overloading the none-parameters' constructors and constructors with default parameters,the program will bring about duality.This situation must be avoided in practice. 2017年4月26日12时 H0座务 第9章关于类和对象的进一步讨 18 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 18 Note: When overloading the none-parameters’ constructors and constructors with default parameters, the program will bring about duality. This situation must be avoided in practice
函数重载与带默认参数的函数 #include <iostream> using namespace std; int main() { int max(inta=5,intb=6,intc=7);/形参有默认值,形参名可以省略 //int max(int,int ) float max(float a=10.1,float b=20.3,float c=30.3);/形参有默认值,形参名可以省略 //float max(float a=10.1,float b=20.3); float a,b,c; //double a,b,c; int d; a=10.1; b=15.2; c=30.2; cut长<"max(a,b,c="<<max(d)<<endl;/输出3个数中的最大者 cout<<"max(a,b)="<<max(a,b)<<endl;/输出2个数中的最大者 returnO; 2017年4月26日12时 H0务 第9章关于类和对象的进一步讨 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 19 #include <iostream> using namespace std; int main( ) { int max(int a=5, int b=6, int c=7);//形参有默认值,形参名可以省略 //int max(int , int ); float max(float a=10.1,float b=20.3,float c=30.3);//形参有默认值,形参名可以省略 //float max(float a=10.1,float b=20.3); float a,b,c; //double a,b,c; int d; a=10.1; b=15.2; c=30.2; cout<<"max(a,b,c)="<<max(d)<<endl; //输出3个数中的最大者 cout<<"max(a,b)="<<max(a,b)<<endl; //输出2个数中的最大者 return 0; }
函数重载与带默认参数的函数 int max(int a,int b,int c) float max(float a,float b,float c) { if(b>a)a=b; if(b>a)a=b; if(c>a)a=c; if(c>a)a=c; return a; return a; int max(int a,int b) float max(float a,float b) { { if(b>a)a=b; if(b>a)a=b; //if(c>a)a=c; //if(c>a)a=c; return a; return a; 2017年4月26日12时 第9章关于类和对象的进一步讨 20 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 20 int max(int a,int b,int c) { if(b>a) a=b; if(c>a) a=c; return a; } int max(int a,int b) { if(b>a) a=b; //if(c>a) a=c; return a; } float max(float a,float b,float c) { if(b>a) a=b; if(c>a) a=c; return a; } float max(float a,float b) { if(b>a) a=b; //if(c>a) a=c; return a; }
Destructor 1.Definition; Destructor is a special member functions,and it perform opposite to the constructor,including the following tasks: Complete some cleaning work before the object being deleted. Automatically recall the destructor by complier system when the survival period ended,and then free up all resource belonging to this object,such as memory space. If destructor hasn't been declared in the program, the complier will automatically create a default destructor. 2017年4月26日12时 H0务 第9章关于类和对象的进一步讨 论 BACK NEXT
HOME2017年4月26日12时 15分 第9章 关于类和对象的进一步讨 论 21 • 1. Definition: • Destructor is a special member functions, and it perform opposite to the constructor, including the following tasks: – Complete some cleaning work before the object being deleted. – Automatically recall the destructor by complier system when the survival period ended, and then free up all resource belonging to this object, such as memory space. • If destructor hasn’t been declared in the program, the complier will automatically create a default destructor