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. vue如何写组件(script标签引入的方式)

    很多人知道.vue结构的单文件组件形式,不过这种单文件组件的结构如果要加入到现有的jquery项目中就比较麻烦了,那如果我们又想用vue来写模板,又不想引入vue-cli管理,那该怎么来写组件呢?别着 ...

  2. SeleniumLibrary 主要关键字 基于python3

    关键字的解释 https://blog.csdn.net/ak739105231/article/details/88926995 click element 都是元素点击事件:不赘述 click l ...

  3. 菜鸡的Java笔记 第二十六 - java 内部类

    /*    innerClass        从实际的开发来看,真正写到内部类的时候是在很久以后了,短期内如果是自己编写代码,几乎是见不到内部类出现的        讲解它的目的第一个是为了解释概念 ...

  4. 我個人喜歡的一些Ubuntu的相關配置

    1.vim vim安裝: sudo apt-get install vim-gtk vim美化:刚安装的VIM,可能界面并不是十分友好,我们可以更改vim的配置文件,按照我们的需求去修改它.在命令行下 ...

  5. 90-95年CPU功耗感知调度研究

    最近读了三篇1990-1995年的通过调度来降低cpu能耗的文章[1] [2] [3],简单总结一下该年代单核CPU功耗感知的调度策略. Motivation 随着便携式设备逐渐兴起,人们对降低其功耗 ...

  6. Spring Boot核心注解

    (1)@SpringBootApplication 代表SpringBoot的启动类 (2)@SpringBootConfiguration 通过bean对象来获取配置信息 (3)@Configura ...

  7. [luogu3292]幸运数字

    考虑点分治,将询问离线后计算重心到每一个点的线性基,然后再询问重心到每一个点的线性基,时间复杂度为$o(3600q)$,可以过(然而太菜的我写了倍增维护线性基,震惊于倍增和线性基常数之小) 1 #in ...

  8. [loj3304]作业题

    (以下假设$T=(V,\{e_{1},e_{2},...,e_{n-1} \})$是一棵树) 根据莫比乌斯反演,有$\gcd(w_{1},w_{2},...,w_{e_{n-1}})=\sum_{d| ...

  9. SpringMVC---Json的使用

    1.所需文件 2.pom中加入json <?xml version="1.0" encoding="UTF-8"?> <web-app xml ...

  10. 文本分类:Keras+RNN vs传统机器学习

    摘要:本文通过Keras实现了一个RNN文本分类学习的案例,并详细介绍了循环神经网络原理知识及与机器学习对比. 本文分享自华为云社区<基于Keras+RNN的文本分类vs基于传统机器学习的文本分 ...