zoukankan      html  css  js  c++  java
  • Output of C++ Program | Set 14

     

      Predict the output of following C++ program.

      Difficulty Level: Rookie

    Question 1

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6     int id;
     7 public:
     8     A (int i) 
     9     { 
    10         id = i; 
    11     }
    12     void print() 
    13     { 
    14         cout << id << endl; 
    15     }
    16 };
    17 
    18 int main()
    19 {
    20     A a[2];
    21     a[0].print();
    22     a[1].print();
    23     return 0;
    24 }

      There is a compilation error in line “A a[2]“. There is no default constructor in class A. When we write our own parameterzied constructor or copy constructor, compiler doesn’t create the default constructor (See this Gfact). We can fix the error, either by creating a default constructor in class A, or by using the following syntax to initialize array member using parameterzied constructor.

    // Initialize a[0] with value 10 and a[1] with 20 
     A a[2] = { A(10),  A(20) } 


    Question 2

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6     int id;
     7     static int count;
     8 public:
     9     A()
    10     {
    11         count++;
    12         id = count;
    13         cout << "constructor called " << id << endl;
    14     }
    15     ~A()
    16     {
    17         cout << "destructor called " << id << endl;
    18     }
    19 };
    20 
    21 int A::count = 0;
    22 
    23 int main()
    24 {
    25     A a[2];
    26     return 0;
    27 }

      Output:

      constructor called 1
      constructor called 2
      destructor called 2
      destructor called 1
      In the above program, object a[0] is created first, but the object a[1] is destroyed first. Objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object.

      For example, consider the following code snippet.

    A a;
    B b(a);

      In the above code, the object ‘b’ (which is created after ‘a’), may use some members of ‘a’ internally. So destruction of ‘a’ before ‘b’ may create problems. Therefore, object ‘b’ must be destroyed before ‘a’.

     

    Question 3

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6     int aid;
     7 public:
     8     A(int x)
     9     { 
    10         aid = x; 
    11     }
    12     void print()
    13     { 
    14         cout << "A::aid = " << aid; 
    15     }
    16 };
    17 
    18 class B
    19 {
    20     int bid;
    21 public:
    22     static A a;
    23     B (int i) 
    24     { 
    25         bid = i; 
    26     }
    27 };
    28 
    29 int main()
    30 {
    31     B b(10);
    32     b.a.print();
    33     return 0;
    34 }

      Compiler Error: undefined reference to `B::a’
      The class B has a static member ‘a’. Since member ‘a’ is static, it must be defined outside the class. Class A doesn’t have Default constructor, so we must pass a value in definition also. Adding a line “A B::a(10);” will make the program work.

      The following program works fine and produces the output as “A::aid = 10″

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class A
     5 {
     6     int aid;
     7 public:
     8     A(int x)
     9     { 
    10         aid = x; 
    11     }
    12     void print()
    13     { 
    14         cout << "A::aid = " <<aid; 
    15     }
    16 };
    17 
    18 class B
    19 {
    20     int bid;
    21 public:
    22     static A a;
    23     B (int i) 
    24     { 
    25         bid = i; 
    26     }
    27 };
    28 
    29 A B::a(10);
    30 
    31 int main()
    32 {
    33     B b(10);
    34     b.a.print();
    35     return 0;
    36 }

      

      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:22:31

  • 相关阅读:
    uva10129 PlayOnWords(并查集,欧拉回路)
    UVA439 knightMoves (A*启发搜索)
    uva297 Quadtrees (线段树思想,区间操作)
    [uva]AncientMessages象形文字识别 (dfs求连通块)
    [uva816]AbbottsRevenge Abbott的复仇(经典迷宫BFS)
    [dp][uestc]L
    [dp][uestc oj]J
    [uestc oj]H
    [dp uestc oj] G
    [dp]uestc oj 邱老师看电影
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3446019.html
Copyright © 2011-2022 走看看