What is the difference between creating class P with all members as private and befriending class B which is derived from P
class P
{
private:
int i;
friend class B;
};
class B: public P
{
public:
void get()
{
cout <<i;
}
};
int main()
{
B obj2;
obj2.get();
return 0;
}
vs
Creating a class within a class so that nobody can use the hidden class(subclass) it would be easy for maintanence without having to worry about breaking someone elses dependent code.
In which situation do we choose which design from the above(or any other if any)?
This question is in continuation to the comment section of answer of Why would one use nested classes in C++?
Don't do what you suggest: its bad design and there's a chance of circular dependencies. Use protected members instead.