Open In App

Output of C++ Program | Set 14

Last Updated : 21 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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





Previous Article
Next Article

Similar Reads

Output of C++ Program | Set 1
Predict the output of below C++ programs. Question 1 // Assume that integers take 4 bytes. #include&lt;iostream&gt; using namespace std; class Test { static int i; int j; }; int Test::i; int main() { cout &lt;&lt; sizeof(Test); return 0; } Output: 4 (size of integer) static data members do not contribute in size of an object. So 'i' is not consider
1 min read
Output of C Program | Set 17
Predict the output of following C programs. Question 1 #include&lt;stdio.h&gt; #define R 10 #define C 20 int main() { int (*p)[R][C]; printf(&quot;%d&quot;, sizeof(*p)); getchar(); return 0; } Output: 10*20*sizeof(int) which is "800" for compilers with integer size as 4 bytes. The pointer p is de-referenced, hence it yields type of the object. In t
2 min read
Output of C++ Program | Set 2
Predict the output of below C++ programs. Question 1 #include&lt;iostream&gt; using namespace std; class A { public: A(int ii = 0) : i(ii) {} void show() { cout &lt;&lt; &quot;i = &quot; &lt;&lt; i &lt;&lt; endl;} private: int i; }; class B { public: B(int xx) : x(xx) {} operator A() const { return A(x); } private: int x; }; void g(A a) { a.show();
2 min read
Output of C++ Program | Set 3
Predict the output of below C++ programs.Question 1 C/C++ Code #include&lt;iostream&gt; using namespace std; class P { public: void print() { cout &lt;&lt;&quot; Inside P::&quot;; } }; class Q : public P { public: void print() { cout &lt;&lt;&quot; Inside Q&quot;; } }; class R: public Q { }; int main(void) { R r; r.print(); return 0; } Output: Insi
2 min read
Output of C Program | Set 18
Predict the output of following C programs.Question 1 C/C++ Code #include&lt;stdio.h&gt; int fun() { static int num = 40; return num--; } int main() { for(fun(); fun(); fun()) { printf(&quot;%d &quot;, fun()); } getchar(); return 0; } Output: 38 35 32 29 26 23 20 17 14 11 8 5 2Since num is static in fun(), the old value of num is preserved for subs
2 min read
Output of C Program | Set 19
Predict the outputs of following program. Difficulty Level: Rookie Question 1 #include &lt;stdio.h&gt; int main() { int a = 10, b = 20, c = 30; if (c &gt; b &gt; a) { printf(&quot;TRUE&quot;); } else { printf(&quot;FALSE&quot;); } getchar(); return 0; } Output: FALSE Let us consider the condition inside the if statement. Since there are two greater
1 min read
Output of C Program | Set 20
Predict the outputs of following C programs. Question 1 int main() { int x = 10; static int y = x; if(x == y) printf(&quot;Equal&quot;); else if(x &gt; y) printf(&quot;Greater&quot;); else printf(&quot;Less&quot;); getchar(); return 0; } Output: Compiler Error In C, static variables can only be initialized using constant literals. See this GFact fo
2 min read
Output of C++ Program | Set 4
Difficulty Level: Rookie Predict the output of below C++ programs. Question 1 #include&lt;iostream&gt; using namespace std; int x = 10; void fun() { int x = 2; { int x = 1; cout &lt;&lt; ::x &lt;&lt; endl; } } int main() { fun(); return 0; } Output: 10 If Scope Resolution Operator is placed before a variable name then the global variable is referen
2 min read
Output of C++ Program | Set 5
Difficulty Level: RookiePredict the output of below C++ programs.Question 1 C/C++ Code #include&lt;iostream&gt; using namespace std; class Test { int value; public: Test(int v); }; Test::Test(int v) { value = v; } int main() { Test t[100]; return 0; } Output: Compiler error The class Test has one user defined constructor "Test(int v)" that expects
2 min read
Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 #include&lt;iostream&gt; using namespace std; class Test { int value; public: Test (int v = 0) {value = v;} int getValue() { return value; } }; int main() { const Test t; cout &lt;&lt; t.getValue(); return 0; } Output: Compiler Error. A const object cannot call a non-const function. The above cod
2 min read
Article Tags :
Practice Tags :