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. Cobar SQL审计的设计与实现

    背景介绍 Cobar简介 Cobar 是阿里开源的一款数据库中间件产品. 在业务高速增长的情况下,数据库往往成为整个业务系统的瓶颈,数据库中间件的出现就是为了解决数据库瓶颈而产生的一种中间层产品. 在 ...

  2. sed tr 批量转换邮箱格式 去除"\n" 行尾添加";"

    1:从phpmyadmin上拿下来的数据是这样的: 2:od -c 发现存在\r\n (windows上编码问题) $ od -c sql.csv 先将\r处理掉 $ sed 's/\r//' sql ...

  3. Linux&c 文件操作,线程进程控制,网络编程,简单知识点梳理

    一:文件操作 在linux下,一切皆文件,目录是文件,称为目录文件,内容是该目录的目录项(但是目录只有内核可以编辑,超级用户也不可以编辑),设备也是设备文件,在/dev存放的就是一些设备文件,linu ...

  4. kubernetes创建用户

    创建k8s User Account 使用openssl方法创建普通用户 准备工作 1 2 3 4 mkdir /root/pki/ 将k8s ca.pem  ca-key.pem 证书拷贝到此目录 ...

  5. MYSQL5.7下载安装图文教程

    MYSQL5.7下载安装图文教程 一. MYSQL两种安装包格式 MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的.zip格式相当于绿色版,不需要安装,只需解压缩之后就可以使用了,但 ...

  6. idea查看方法在哪里被调用

    方法一 选中方法名,右键选择Find Usages 方法二 选中方法快捷键ctrl + alt + h查看Hierarchy Callers

  7. 谷粒 | 10 | 阿里云OSS存储对象服务

    阿里云OSS对象存储服务 准备工作 1.在service模块新建子模块service_oss 2.引入pom.xml文件中引入oss服务依赖 <dependencies> <!--a ...

  8. K8S 部署 SpringBoot 项目(一篇够用)

    现在比较多的互联网公司都在尝试将微服务迁到云上,这样的能够通过一些成熟的云容器管理平台更为方便地管理微服务集群,从而提高微服务的稳定性,同时也能较好地提升团队开发效率. 但是迁云存在一定的技术难点,今 ...

  9. Java学习(二十一)

    今天学的访问控制权限修饰符: 和c++比多了一个缺省,基本和c++功能一样,private多了一个同包的限制. 缺省的话是同包就可访问. 基本都学过,所以理解的也很快. 然后又把最近学得总结了一下: ...

  10. SQL 添加列,删除列,修改列的类型

    alter table 表名 add 列名 数据类型 如:alter table student add nickname char(20) alter table tableName(表名) add ...