Output of C++ Program | Set 13
Predict the output of following C++ program.
1 #include<iostream>
2 using namespace std;
3
4 class A
5 {
6 // data members of A
7 public:
8 A ()
9 {
10 cout << "\n A's constructor"; /* Initialize data members */
11 }
12 A (const A &a)
13 {
14 cout << "\n A's Copy constructor"; /* copy data members */
15 }
16 A& operator= (const A &a) // Assignemt Operator
17 {
18 // Handle self-assignment:
19 if(this == &a)
20 {
21 return *this;
22 }
23 // Copy data members
24 cout << "\n A's Assignment Operator";
25 return *this;
26 }
27 };
28
29 class B
30 {
31 A a;
32 // Other members of B
33 public:
34 B(A &a)
35 {
36 this->a = a;
37 cout << "\n B's constructor";
38 }
39 };
40
41 int main()
42 {
43 A a1;
44 B b(a1);
45 return 0;
46 }
Output:
A's constructor
A's constructor
A's Assignment Operator
B's constructor
The first line of output is printed by the statement “A a1;” in main().
The second line is printed when B’s member ‘a’ is initialized. This is important.
The third line is printed by the statement “this->a = a;” in B’s constructor.
The fourth line is printed by cout statement in B’s constructor.
If we take a look a closer look at the above code, the constructor of class B is not efficient as member ‘a’ is first constructed with default constructor, and then the values from the parameter are copied using assignment operator. It may be a concern when class A is big, which generally is the case with many practical classes.
See the following optimized code.
1 #include<iostream>
2 using namespace std;
3
4 class A
5 {
6 // data members of A
7 public:
8 A()
9 {
10 cout << "\n A's constructor"; /* Initialize data members */
11 }
12 A(const A &a)
13 {
14 cout << "\n A's Copy constructor"; /* Copy data members */
15 }
16 A& operator= (const A &a) // Assignemt Operator
17 {
18 // Handle self-assignment:
19 if(this == &a)
20 return *this;
21
22 // Copy data members
23 cout << "\n A's Assignment Operator";
24 return *this;
25 }
26 };
27
28 class B
29 {
30 A a;
31 // Other members of B
32 public:
33 B(A &a):a(a)
34 {
35 cout << "\n B's constructor";
36 }
37 };
38
39 int main()
40 {
41 A a;
42 B b(a);
43 return 0;
44 }
Output:
A's constructor
A's Copy constructor
B's constructor
The constructor of class B now uses initializer list to initialize its member ‘a’. When Initializer list is used, the member ‘a’ of class B is initialized directly from the parameter. So a call to A’s constructor is reduced.
In general, it is a good idea to use Initializer List to initialize all members of a class, because it saves one extra assignment of members. See point 6 of this post for more details.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-27 16:13:26
Output of C++ Program | Set 13的更多相关文章
- Output of C++ Program | Set 18
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 17
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 16
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 15
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 14
Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...
- Output of C++ Program | Set 11
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 9
Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...
- Output of C++ Program | Set 7
Predict the output of following C++ programs. Question 1 1 class Test1 2 { 3 int y; 4 }; 5 6 class T ...
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...
随机推荐
- 基于 OSPF 路由的邻居邻接关系发现实践
1.实验目的 理解 OSPF 邻居关系和 OSPF 邻接关系的含义及差别 观察 OSPF 邻居邻接关系的建立过程 观察 OSPF 链路状态数据库的同步过程 2.实验原理 OSPF 网络中,路由器在发送 ...
- TCP/IP概述(网络互联与TCP/IP)
TCP/IP概述(网络互联与TCP/IP) 用IP实现异构网络互联 从用户角度如何实现异构网络互联: 从用户角度看,实现异构网络互联的关键点就是使各种网络类型之间的差异对自己透明.在TCP/IP协议中 ...
- jQuery淡入淡出效果
如果是通过鼠标点击事件来触发动画效果可以使用 $("#button").click(function(){ $("#div").stop().fadeToggl ...
- robotframework-ride快捷方式打不开
我安装的是最新的RIDE2.0属于beta测试中,覆盖了3.8但仍不支持3.9 我的安装环境如下: 安装ride成功,启动ride的时候遇到了如下问题: 一:AttributeError: No at ...
- 讲分布式唯一id,这篇文章很实在
分布式唯一ID介绍 分布式系统全局唯一的 id 是所有系统都会遇到的场景,往往会被用在搜索,存储方面,用于作为唯一的标识或者排序,比如全局唯一的订单号,优惠券的券码等,如果出现两个相同的订单号,对于用 ...
- 测试开发【提测平台】分享14-Vue图标Icon几种用法并利用其一优化菜单
微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 回归主线更新,由于本次知识点只有一个,就不给思维导图了,在上系列测试平台开发实践中主要学习了页面直接的转跳方法和远程搜索的如何做,最终实现 ...
- 线性规划之单纯形算法矩阵描述与python实现
声明 本文为本人原创,转载请注明出处.本文仅发表在博客园,作者LightningStar. 问题描述 所有的线性规划问题都可以归约到标准型的问题,规约过程比较简单且已经超出本文范围,不再描述,可以参考 ...
- Linux系统查看磁盘可用空间的5个命令
大家好,我是良许. 工作中,经常会遇到磁盘爆满的情况,尤其是一台服务器运行了 N 年之后,里面会充满各种各样垃圾文件,比如:编译产生的中间文件.打包的镜像文件.日志文件,等等. 别问我怎么知道,我上家 ...
- Java设计模式之(四)——原型模式
1.什么是原型模式 Specify the kinds of objects to create using a prototypical instance,and create new object ...
- (数据科学学习手札131)pandas中的常用字符串处理方法总结
本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 在日常开展数据分析的过程中,我们经常需要对 ...