Open In App

Output of C++ Program | Set 2

Last Updated : 10 Jul, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Predict the output of below C++ programs.

Question 1




#include<iostream>
using namespace std;
 
class A { 
 public:
    A(int ii = 0) : i(ii) {}
    void show() { cout << "i = " << i << 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(); }
 
int main() {
  B b(10);
  g(b);
  g(20);
  getchar();
  return 0;


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




#include<iostream>
using namespace std;
  
class base {
    int arr[10];     
};
  
class b1: public base { };
  
class b2: public base { };
  
class derived: public b1, public b2 {};
  
int main(void)
  cout<<sizeof(derived);
  getchar();
  return 0;
}


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 (See this for details)




#include<iostream>
using namespace std;
  
class base {
  int arr[10];     
};
  
class b1: virtual public base { };
  
class b2: virtual public base { };
  
class derived: public b1, public b2 {};
  
int main(void)
  cout<<sizeof(derived);
  getchar();
  return 0;
}


Please write comments if you find any of the answers/explanations incorrect, or you want to share more information about the topics discussed above



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 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
Output of C Program | Set 21
Predict the output of following C programs. Question 1 C/C++ Code #include&amp;lt;stdio.h&amp;gt; #define fun (x) (x)*10 int main() { int t = fun(5); int i; for(i = 0; i &amp;lt; t; i++) printf(&amp;quot;GeeksforGeeks\n&amp;quot;); return 0; } Output: Compiler Error There is an extra space in macro declaration which causes fun to be replaced by (x)
2 min read
Article Tags :