אם ההורשה היא public אז כן
בגדול זה עובד ככה:
class A
{
public:
int i1;
virtual int f1()
{
return 1;
}
int f2()
{
return 1;
}
virtual f3()
{
return 1;
}
protected:
int i2;
private:
int i3;
}
class B : public A
{
public:
virtual int f1()
{
return 2;
}
int f2()
{
// return i3; // Will not work, i3 is private.
return i2; // OK, since i2 is protected
}
}
A a;
B b;
A *ap = new B();
B *bp = new B();
a.i1 = 0; // OK
b.i1 = 0; // OK
ap->i1 = 0; // OK
bp->i1 = 0; // OK
// a.i2 = 0; // Will not work, i2 is protected.
a.f1(); // 1
a.f2(); // 1
a.f3(); // 1
ap->f1(); // 1
ap->f2(); // 1
ap->f3(); // 1
b.f1(); // 2
b.f2(); // i2
b.f3(); // 1
bp->f1(); // 2. f1 is virtual
A::f1(bp); // 1
bp->f2(); // 1, f2 is not virtual
bp->f3(); // 1, f3 is not overrided