Open In App

Output of C++ Program | Set 15

Last Updated : 27 Dec, 2016
Improve
Improve
Like Article
Like
Save
Share
Report

Predict the output of following C++ programs.

Question 1




#include <iostream>
using namespace std;
  
class A
{
public:
    void print() { cout << "A::print()"; }
};
  
class B : private A
{
public:
    void print() { cout << "B::print()"; }
};
  
class C : public B
{
public:
    void print() { A::print(); }
};
  
int main()
{
    C b;
    b.print();
}


Output: Compiler Error: ‘A’ is not an accessible base of ‘C’
There is multilevel inheritance in the above code. Note the access specifier in “class B : private A”. Since private access specifier is used, all members of ‘A’ become private in ‘B’. Class ‘C’ is a inherited class of ‘B’. An inherited class can not access private data members of the parent class, but print() of ‘C’ tries to access private member, that is why we get the error.



Question 2




#include<iostream>
using namespace std;
  
class base
{
public:
    virtual void show()  { cout<<" In Base \n"; }
};
  
class derived: public base
{
    int x;
public:
    void show() { cout<<"In derived \n"; }
    derived()   { x = 10; }
    int getX() const { return x;}
};
  
int main()
{
    derived d;
    base *bp = &d;
    bp->show();
    cout << bp->getX();
    return 0;
}


Output: Compiler Error: ‘class base’ has no member named ‘getX’
In the above program, there is pointer ‘bp’ of type ‘base’ which points to an object of type derived. The call of show() through ‘bp’ is fine because ‘show()’ is present in base class. In fact, it calls the derived class ‘show()’ because ‘show()’ is virtual in base class. But the call to ‘getX()’ is invalid, because getX() is not present in base class. When a base class pointer points to a derived class object, it can access only those methods of derived class which are present in base class and are virtual.



Question 3




#include<iostream>
using namespace std;
  
class Test
{
    int value;
public:
    Test(int v = 0) { value = v; }
    int getValue()  { return value; }
};
  
int main()
{
    const Test t;
    cout << t.getValue();
    return 0;
}


Output: Compiler Error
In the above program, object ‘t’ is declared as a const object. A const object can only call const functions. To fix the error, we must make getValue() a const function.



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 :