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的更多相关文章

  1. Output of C++ Program | Set 18

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  2. Output of C++ Program | Set 17

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  3. Output of C++ Program | Set 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  4. Output of C++ Program | Set 15

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  5. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  6. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  7. Output of C++ Program | Set 9

    Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...

  8. 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 ...

  9. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. PTA 6-1 求采用邻接矩阵作为存储结构的无向图各顶点的度 (6分)

    PTA 6-1 求采用邻接矩阵作为存储结构的无向图各顶点的度 (6分) 函数接口定义: 函数接口为: void dgree(MGraph G); G为采用邻接矩阵作为存储结构的有向图 裁判测试程序样例 ...

  2. freeswitch APR库

    概述 freeswitch依赖库源代码基本都可以在libs目录下找到. 在freeswitch的官方手册中,可以找到freeswitch的依赖库表格,其中freeswitch的core核心代码依赖库主 ...

  3. [linux]centos7.4上升级python2版本到python3.6.5 【安装双版本,默认python3】

    版本声明 centos7.4 前言:linux上的python默认是版本2的,之前学django项目用的是3的版本 所以得升级下版本~ 1.下载python3.6.5 cd /usr/local/ w ...

  4. JAVA学习(七)

    今天讲师又讲了一个多小时类的注意点,例如书写格式什么的,这些我c++中都学过了,所以很快看完. 然后又讲了IDE,我选择的是IntelliJ IDEA. 刚开始官网登不上去,花了一个小时,从网上翻了各 ...

  5. difflib模块详解

    1.两个字符串对比 import difflib text1=""" test1 #定义字符串 hellow my name is machanwei! difflib ...

  6. PAT A1091——BFS

    Acute Stroke One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. ...

  7. [cf1515G]Phoenix and Odometers

    显然这条路径只能在$v_{i}$所在的强连通分量内部,不妨仅考虑这个强连通分量 对这个强连通分量dfs,得到一棵外向树(不妨以1为根) 考虑一条边$(u,v,l)$,由于强连通,总存在一条从$v$到$ ...

  8. [cf1236F]Alice and the Cactus

    首先,我们要用到期望的一个性质: 对于两个随机变量$X$和$Y$(不需要相互独立),有$E(X+Y)=E(X)+E(Y)$ 另外,对于一个仙人掌,令$n$为点数,$m$为边数,$c$为简单环个数,$X ...

  9. [atARC075F]Mirrored

    假设$n=\sum_{i=0}^{k}a_{i}10^{i}$(其中$a_{k}>0$),则有$d=f(n)-n=\sum_{i=0}^{k}(10^{k-i}-10^{i})a_{i}$,考虑 ...

  10. Study Blazor .NET(四)数据绑定

    翻译自:Study Blazor .NET,转载请注明. 数据绑定 单向绑定 在blazor中单向绑定简单而直接,无需UI刷新或渲染.下面示例展示了单向数据绑定: //Counter.razor @p ...