Skip to content
Related Articles

Related Articles

Improve Article
Output of C++ Program | Set 14
  • Difficulty Level : Medium
  • Last Updated : 28 Sep, 2018

Predict the output of following C++ program.

Difficulty Level: Rookie

Question 1




#include <iostream>
using namespace std;
  
class A
{
    int id;
public:
    A (int i) { id = i; }
    void print () { cout << id << endl; }
};
  
int main()
{
    A a[2];
    a[0].print();
    a[1].print();
    return 0;
}

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






#include <iostream>
using namespace std;
  
class A
{
    int id;
    static int count;
public:
    A()
    {
        count++;
        id = count;
        cout << "constructor called " << id << endl;
    }
    ~A()
    {
        cout << "destructor called " << id << endl;
    }
};
  
int A::count = 0;
  
int main()
{
    A a[2];
    return 0;
}

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




#include <iostream>
using namespace std;
  
class A
{
   int aid;
public:
   A(int x)
   { aid = x; }
   void print()
   { cout << "A::aid = " <<aid; }
};
  
class B
{
    int bid;
public:
    static A a;
    B (int i) { bid = i; }
};
  
int main()
{
  B b(10);
  b.a.print();
  return 0;
}

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”




#include <iostream>
using namespace std;
  
class A
{
   int aid;
public:
   A(int x)
   { aid = x; }
   void print()
   { cout << "A::aid = " <<aid; }
};
  
class B
{
    int bid;
public:
    static A a;
    B (int i) { bid = i; }
};
  
A B::a(10);
  
int main()
{
  B b(10);
  b.a.print();
  return 0;
}



Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :