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. 五(一)、spring 声明式事务注解配置

    一.事务概述: 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用:比如 用户购买图书:购买动作之前需要确认 ①图书的数量是否足够:②用户账号余额是否足够 ...

  2. webpack 提取css成单独文件

    webpack 提取css成单独文件 // 用来拼接绝对路径的方法 const {resolve} = require('path') const HtmlWebpackPlugin = requir ...

  3. Django笔记&教程 1-2 二 常用配置

    Django 自学笔记兼学习教程第1章第2节--二 常用配置 点击查看教程总目录 新手建议简单浏览本文,不理解的建议跳过,不要强行理解. Django的设置涉及多个模块,需要了解Django的一些相关 ...

  4. Django笔记&教程 5-2 进阶查询——Queryset

    Django 自学笔记兼学习教程第5章第2节--进阶查询--Queryset 点击查看教程总目录 Queryset相关内容其实蛮多的,本文只介绍一些常用的,详细的推荐查询官方文档:queryset-a ...

  5. Centos8 部署 ElasticSearch 集群并搭建 ELK,基于Logstash同步MySQL数据到ElasticSearch

    Centos8安装Docker 1.更新一下yum [root@VM-24-9-centos ~]# yum -y update 2.安装containerd.io # centos8默认使用podm ...

  6. js防止重复提交代码

    if (checkSubmitFlg == true) { console.log("禁止频繁操作.."); layer.close(ide); return false; } c ...

  7. 32、最长有效括号 | 算法(leetode,附思维导图 + 全部解法)300题

    零 标题:算法(leetode,附思维导图 + 全部解法)300题之(32)最长有效括号 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 "滑动窗 ...

  8. C/C++ Qt 给ListWidget增加右键菜单

    在上一篇博文<C/C++ Qt ListWidget 列表框组件应用>中介绍了ListWidget组件的基本使用技巧,本次将给ListWidget组件增加一个右键菜单,当用户在ListWi ...

  9. 【BZOJ 4668 冷战】

    题目: [BZOJ 4668 冷战] 思路: 因为考虑强制在线,我们是肯定要维护形状的 我们发现如果\((u,v)\)这条边如果\(u,v\)已经连上,那么对于最终答案这条边是没有贡献的 所以我们发现 ...

  10. NFLSOJ #10317. -「2020联考北附2」三千世界(找等价表达+树形 dp)

    题面传送门 出题人可能原本感觉没啥难度的 T2 竟然变成了防 AK 题,奇迹奇迹( 首先带着这个 \(\max\) 肯定不太好处理,考虑找出 \(f(S)\) 的等价表达.我们考虑以 \(1\) 为根 ...