|
class parent
{
public:
int i1;
void f1()
{
cout << "public parent method" << endl;
}
private:
int i2;
void f2()
{
cout << "private parent method" << endl;
}
};
class child : public parent
{
public:
void worker()
{
i1 = 1; // Allowed
f1();
i2 = 1; // Forbidden...
f2();
}
};
|
So there is an access qualifier to allow only methods in the child class to access the variables in the parent class
This access qualifier is:
protected |
class parent
{
protected:
int i2;
void f2()
{
cout << "protected parent method" << endl;
}
};
class child : public parent
{
public:
void worker()
{
i2 = 1; // Child class can access protected members
// in parent class
f2();
}
};
int main(int argc, char **argv)
{
child x;
parent y;
x.worker();
y.f2(); // Forbidded...
}
|
|