Types of C++ Inheritance: public protected private

In C++ there are three types of inheritance:

  • public
  • protected
  • private

Any of these three types of inheritance can be modified with the virtual keyword. In my experience interviewing candidates for c++ positions, I've learned that the average programmer does not know how these are used, or even what they mean. So I thought I would go over them here.

The three access modifiers public, protected and private are analogous to the access modifieres used for class members.

public
When a base class is specified as public ie: class c : public base {}; the base class can be seen by anyone who has access to the derived class. That is, any members inherited from base can be seen by code accessing c.
protected
When a base class is specified as protected ie: class c : protected base {}; the base class can only be seen by subclasses of C.
private
When a base class is specified as private ie: class c : private base {}; the base class can only be seen by the class C itself.

Examples of how this plays out:


struct X {
public:
void A() {}
};

struct Y {
public:
void B() {}
};

struct Z {
public:
void C() {}
};

struct Q : public X, protected Y, private Z {
public:
void Test()
{
A(); // OK
B(); // OK
C(); // OK
}
};

struct R : public Q {
public:
void Test2()
{
A(); // OK
B(); // OK
C(); // NOT OK

Q t;
Y *y = &t // OK
Z *z = &t // NOT OK
}
};

int main(int argc, char **argv) {
Q t1;
t1.A(); // OK
t1.B(); // NOT OK
t1.C(); // NOT OK

R t2;
t2.A(); // OK
t2.B(); // NOT OK
t2.C(); // NOT OK

X *x = &t1; // OK
Y *y = &t1; // NOT OK
Z *z = &t1; // NOT OK

x = &t2; // OK
y = &t2; // NOT OK
z = &t2; // NOT OK

}




What about Virtual?

Oh right. Virtual is only useful when multiple inheritance is involved and the same class appears in the inheritance graph more than once. If the inheritance is declared as virtual all instances of the class are merged into one sub object and that sub object is initialized once. If the class that appears multiple times in the inheritance graph is NOT declared virtual one sub object is created for EACH instance of the class and the class is initialized multiple times.

Be careful! If the class is inherited sometimes as virtual and sometimes not, the virtual instances are merged and the non-virtual instances are not, giving you a mix of behavior.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值