For given nested classes in C++, what will be the output of the following code:
#include <iostream> 
using namespace std;

class A {
    int a;
    int getB() {
        return B::b;
    class B {
                B() {
            b = 1;
        int b;

int main() 
    A a;
    cout << a.getB();
    return 0;
_Non-static_ class fields can only be accessed through a class instance.
In member function 'int A::getB()':
 error: invalid use of non-static data member 'A::B::b'
         return B::b;

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Sign Up Now
or Subscribe for future quizzes