Hi All
I have one question. I was trying to test private inheritance and saw something which I need some help to understand.
Code:
class Base
{
int age;
public:
Base(int x): age(x) {}
int getAge()
{
return age;
}
};
class Derived : private Base
{
string name;
public:
Derived(string x,int y): Base(y),name(x) {}
void getName()
{
cout<<"Name is="<<name<<"And Age is="<<getAge();
cout<<age; // Compiler error as Age no accessible
}
};
int main()
{
B b;
b.get();
Derived d("nickman",12);
d.getName();
return 0;
}
So I assume that in private inheritance, all base class data members and functions inherited as private to Derived class and can be access via Derived class functions.
getAge() call from Derived class works fine but compiler give error when I accessed "age" data member.
So question is, if we have inherited privately, then how getAge() call is working fine?
Thanks