zoukankan      html  css  js  c++  java
  • 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 << "
     A's constructor"; /* Initialize data members */ 
    11     }
    12     A (const A &a) 
    13     { 
    14         cout << "
     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 << "
     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 << "
     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 << "
     A's constructor"; /* Initialize data members */ 
    11     }
    12     A(const A &a) 
    13     { 
    14         cout << "
     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 << "
     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 << "
     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

  • 相关阅读:
    27 Spring Cloud Feign整合Hystrix实现容错处理
    26 Spring Cloud使用Hystrix实现容错处理
    25 Spring Cloud Hystrix缓存与合并请求
    24 Spring Cloud Hystrix资源隔离策略(线程、信号量)
    23 Spring Cloud Hystrix(熔断器)介绍及使用
    22 Spring Cloud Feign的自定义配置及使用
    21 Spring Cloud使用Feign调用服务接口
    20 Spring Cloud Ribbon配置详解
    19 Spring Cloud Ribbon自定义负载均衡策略
    18 Spring Cloud Ribbon负载均衡策略介绍
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445963.html
Copyright © 2011-2022 走看看