• Output of C++ Program | Set 2


      Predict the output of below C++ programs.

      Question 1

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class A 
     5 { 
     6 public:
     7     A(int ii = 0) : i(ii) 
     8     {
     9     }
    10     void show() 
    11     { 
    12         cout << "i = " << i << endl;
    13     }
    14 private:
    15     int i;
    16 };
    17 
    18 class B 
    19 {
    20 public:
    21     B(int xx) : x(xx) 
    22     {
    23     }
    24     operator A() const 
    25     { 
    26         return A(x); 
    27     }
    28 private:
    29     int x;
    30 };
    31 
    32 void g(A a)
    33 {  
    34     a.show(); 
    35 }
    36 
    37 int main() 
    38 {
    39     B b(10);
    40     g(b);
    41     g(20);
    42     getchar();
    43     return 0;
    44 } 

      Output:
      i = 10
      i = 20

      Since there is a Conversion constructor in class A, integer value can be assigned to objects of class A and function call g(20) works. Also, there is a conversion operator overloaded in class B, so we can call g() with objects of class B.

     

      Question 2

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class base 
     5 {
     6     int arr[10];     
     7 };
     8 
     9 class b1: public base 
    10 { 
    11 };
    12 
    13 class b2: public base 
    14 { 
    15 };
    16 
    17 class derived: public b1, public b2 
    18 {
    19 };
    20 
    21 int main(void)
    22 { 
    23     cout << sizeof(derived);
    24     getchar();
    25     return 0;
    26 }

      Output: If integer takes 4 bytes, then 80.

      Since b1 and b2 both inherit from class base, two copies of class base are there in class derived. This kind of inheritance without virtual causes wastage of space and ambiguities. virtual base classes are used to save space and avoid ambiguities in such cases.

      For example, following program prints 48. 8 extra bytes are for bookkeeping information stored by the compiler.

     1 #include<iostream>
     2 using namespace std;
     3  
     4 class base 
     5 {
     6   int arr[10];     
     7 };
     8  
     9 class b1: virtual public base 
    10 { 
    11 };
    12  
    13 class b2: virtual public base 
    14 { 
    15 };
    16  
    17 class derived: public b1, public b2 
    18 {
    19 };
    20  
    21 int main(void)
    22 { 
    23   cout << sizeof(derived);
    24   getchar();
    25   return 0;
    26 }

      注意对另外的8个字节的理解哦。

      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  15:00:33

  • 相关阅读:
    Python 向列表中添加元素
    [python] 查找列表中重复的元素
    Excel文件的读写
    Oracle EBS AP 取消付款
    Oracle EBS AP取消核销
    Oracle EBS AP 创建贷项通知单并核销到相应发票
    Oracle EBS AR 其他API
    Oracle EBS AR 冲销收款
    Oracle EBS 银行账户API
    Oracle EBS AR 客户API
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445712.html
Copyright © 2020-2023  润新知