Output of C++ Program | Set 14

Predict the output of following C++ program.

Difficulty Level: Rookie

Question 1

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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



filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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”

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right




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



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.