在构造函数中使用初始化表

刚接触初始化表的时候有点茫然,慢慢的我也熟悉了,初始化表,初始化表可以减少代码量提高编程效率

抛砖引玉:先从一个例子说明初始化表的作用,

定义一个对象数组并且使用构造函数对其初始化

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class Box//Box类  
  6. {  
  7. public:  
  8.   
  9.     //构造函数  
  10.     Box(int h, int w, int l);  
  11.   
  12.     int volume();//计算Box的面积  
  13.   
  14. private:  
  15.     int height;  
  16.     int width;  
  17.     int length;  
  18. };  
  19.   
  20. //构造函数  
  21. Box::Box(int h, int w, int l)  
  22. {  
  23.     height = h;  
  24.     width = w;  
  25.     length = l;  
  26. }  
  27.   
  28. //计算体积  
  29. int Box::volume()  
  30. {  
  31.     int v;  
  32.     v = height * width * length;  
  33.   
  34.     return v;  
  35. }  
  36.   
  37. void main()  
  38. {  
  39.     Box a[3] = {  
  40.         Box(15,18,20),  
  41.         Box(10,12,15),  
  42.         Box(16,20,26)  
  43.     };  
  44.   
  45.     cout<<"volume of a[0] is "<<a[0].volume()<<endl;  
  46.     cout<<"volume of a[1] is "<<a[1].volume()<<endl;  
  47.     cout<<"volume of a[2] is "<<a[2].volume()<<endl;  
  48.   
  49.     system("pause");  
  50. }  

执行结果:


将构造函数改成初始化表的形式后的代码:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class Box//Box类  
  6. {  
  7. public:  
  8.   
  9.     //以初始化表的形式表示构造函数  
  10.     Box(int h, int w, int l):height(h),width(w),length(l){}  
  11.   
  12.     int volume();//计算Box的面积  
  13.   
  14. private:  
  15.     int height;  
  16.     int width;  
  17.     int length;  
  18. };  
  19.   
  20. //计算体积  
  21. int Box::volume()  
  22. {  
  23.     int v;  
  24.     v = height * width * length;  
  25.   
  26.     return v;  
  27. }  
  28.   
  29. void main()  
  30. {  
  31.     Box a[3] = {  
  32.         Box(15,18,20),  
  33.         Box(10,12,15),  
  34.         Box(16,20,26)  
  35.     };  
  36.   
  37.     cout<<"volume of a[0] is "<<a[0].volume()<<endl;  
  38.     cout<<"volume of a[1] is "<<a[1].volume()<<endl;  
  39.     cout<<"volume of a[2] is "<<a[2].volume()<<endl;  
  40.   
  41.     system("pause");  
  42. }  

执行结果:


通过上面两段代码的比较发现两段代码的执行结果一样,而两段代码的不同点就是第一段代码使用的是普通的构造函数,而第二段代码在构造函数中使用了初始化表的形式

第一段使用普通的构造函数的代码:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Box::Box(int h, int w, int l)  
  2. {  
  3.     height = h;  
  4.     width = w;  
  5.     length = l;  
  6. }  


使用初始化表的形式的代码:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //以初始化表的形式表示构造函数  
  2. Box(int h, int w, int l):height(h),width(w),length(l){}  

通过比较发现使用初始化表的形式和使用普通的构造函数的形式可以实现相同的功能,而使用初始化表的代码量比普通构造函数的代码量少只有一行,所以推荐在构造函数中使用初始化表
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值