As by default, each member of class 1 is private , getN in the base class is declared private .
Make getN publicly available as:
class Base { friend class SubClass; int n; public:
1. I mean the class defined with the class keyword. Note that a class can be defined using the struct and union keywords, in accordance with the C ++ standard.
EDIT:
If you think because SubClass is a friend of Base , so it can access private members of Base from the outside, then this is wrong. friend means that SubClass member SubClass can access private members of the Base class.
However, if you make main() friend of Base , then your code will work:
class Base { friend int main();
Now from main() you can access any private Base members!
Watch this demo: http://www.ideone.com/UKkCF
Nawaz source share