3.1构造函数 三、构造函数的创建 (1)无参数的构造函数 (2)带参数的构造函数 在无参数的构造函数中,每一个对象都得到同一组初 值,如果用户希望对不同的对象赋予不同的初值,可以采用 带参数的构造函数。 ◆声明格式为: 构造函数名(类型1形参1,类型2 形参2,): ◆定义一个对象的一般格式为: 类名对象名(实参1,实参2,.); 练习2请写一个包含无参数和带参数构造函数的类 10
(1) 无参数的构造函数 (2) 带参数的构造函数 在无参数的构造函数中,每一个对象都得到同一组初 值,如果用户希望对不同的对象赋予不同的初值,可以采用 带参数的构造函数。 ◆ 声明格式为: 构造函数名 ( 类型1 形参1,类型2 形参2, .); ◆ 定义一个对象的一般格式为: 类名 对象名 ( 实参1,实参2, . ); 练习2 请写一个包含无参数和带参数构造函数的类 3.1 构造函数 三、构造函数的创建 ⚫10
① #include<iostream> ②using namespace std; ③ class Time public:Time();Time(int,int,int); ⑤void displayTime() 练5.3 ⑧ {cout<<"hour:"<<hour<<endl; Time ⑦ cout<<"minute:"<<minute<<endl; 类的 ⑧ cout<<"second:"<<second<<endl; 带参 ⑨ private: 构造 ⑩ int hour; int minute; int second;} ① Time:Time() 函数 hour=12;minute=0;second=0; Time:Time(inth,int m,ints) hour=h;minute=m;second=s; ⑤int main() ⑥{Timet1; ⑩ t1.displayTime(); ® Timet2(10,10,10); 11 ⑨ t2.displayTime();
① #include<iostream> ② using namespace std; ③ class Time ④ { public: Time(); Time(int, int, int); ⑤ void displayTime() ⑥ {cout<<"hour:"<<hour<<endl; ⑦ cout<<"minute:"<<minute<<endl; ⑧ cout<<"second:"<<second<<endl; } ⑨ private: ⑩ int hour; int minute; int second; }; ⑪ Time::Time() ⑫ { hour=12; minute=0; second=0; } ⑬ Time::Time(int h, int m, int s) ⑭ { hour=h; minute=m; second=s; } ⑮ int main() ⑯ { Time t1; ⑰ t1.displayTime(); ⑱ Time t2(10,10,10); ⑲ t2.displayTime(); } 练5.3 Time 类的 带参 构造 函数 ⚫11
3.1构造函数-三、构造函数的创建 练习3有两个长方体,长宽高分别为(1,2,3)和(4,5,6)。试 编写一基于对象的程序,分别求它们的体积,并且要求用 带参数的构造函数初始化它们。 1. include<iostream.h>∥教材例3.2 1 int main ( 2. class box 2. {boxbox1(1,2,3); 3. private: 3. 定义并初始化对象 4. int height,width,length; 4. cout<<"box1的体积=" 5. public: 5. <<box1.volume()<<endl; 6. box(int h,intw,int len)//构造函数 7. o box box2(4,5,6)月 height=h;width=w; 7. 8. Ⅱ定义并初始化对象 length len; 9. } 8. cout<<"box2的体积=" 10. int volume() 9. <<box2.volume() 11. return height width length; 10. <endl; 12.}分 11
练习3 有两个长方体,长宽高分别为(1,2,3)和(4,5,6)。试 编写一基于对象的程序,分别求它们的体积,并且要求用 带参数的构造函数初始化它们。 1. #include <iostream.h> //教材例3.2 2. class box 3. { private: 4. int height, width, length; 5. public: 6. box(int h,int w,int len)// 构造函数 7. { height = h; width = w; 8. length = len; 9. } 10. int volume ( ) 11. { return height * width * length; } 12. }; 1. int main ( ) 2. { box box1 (1,2,3); 3. //定义并初始化对象 4. cout <<"box1的体积=" 5. <<box1.volume( )<< endl; 6. box box2 (4,5,6); 7. //定义并初始化对象 8. cout <<"box2的体积=" 9. <<box2.volume( ) 10. << endl; 11. } 3.1 构造函数-三、构造函数的创建 ⚫12
3.1构造函数-三、构造函数的创建 (3)用参数初始化表对数据成员初始化 C++提供另一种初始化数据成员的方 法:参数初始化表来实现对数据成员的初 始化。这种方法不在构造函数体内初始化 数据成员,而是在函数首部实现。 方便简练 练习4:将练习3改写成如下形式: e13
(3)用参数初始化表对数据成员初始化 C++提供另一种初始化数据成员的方 法:参数初始化表来实现对数据成员的初 始化。这种方法不在构造函数体内初始化 数据成员,而是在函数首部实现。 方便 简练 练习4:将练习3改写成如下形式: 3.1 构造函数-三、构造函数的创建 ⚫13
1.include<iostream.h>∥练习4 2. class box 3.private:int height,width,length; 4. public: 5. box(int h,int w,int len):height(h),width(w),length(len){} 6. int volume() 7. return height*width*length;} 8.} 9.void main() 10.{box box1(1,2,3); 11. cout<"box1的体积="<box1.volume()<<endl; 12. box box2(4,5,6); 13. cout<"box2的体积="<box2.volume()<<endl; 14.} 14
1. #include <iostream.h> // 练习4 2. class box 3. { private: int height, width, length; 4. public: 5. box(int h,int w,int len):height(h),width(w),length(len) { } 6. int volume ( ) 7. { return height * width * length; } 8. }; 9. void main ( ) 10. { box box1 (1,2,3); 11. cout <<"box1的体积=" <<box1.volume( ) << endl; 12. box box2 (4,5,6); 13. cout <<"box2的体积="<<box2.volume( ) << endl; 14. } ⚫14