+2 votes
in Class 12 by kratos

Identify the errors in the following code fragment. Discuss the responsible reasons for them.

int glob;

class F {

int glob;

public:

void readit()

{ cin>>glob;

}

};

class W:public F {

public:

void test()

{

glob--; }

};

1 Answer

+4 votes
by kratos
 
Best answer

The compiler will not allow the class W to access F::glob as well as global glob because F::glob being private to F cannot be accessed directly by the class W, and global glob cannot be accessed in W as it is hidden here because F::glob is visible here but inaccessible.

Above error can be solved by writing following statement in test() method:

::glob--;

...