C++中的继承

来源:互联网 发布:foreach去除数组重复值 编辑:程序博客网 时间:2024/06/07 10:25

1.继承的方式

有三种,public,protected和private。

public继承中,父类的public成员和protected成员继承到子类中成为public成员和protected成员。父类中的private成员在子类中不可访问。

protected继承中,父类的public成员和protected成员继承到子类中成为protected成员和protected成员。父类中的private成员在子类中不可访问。

private继承中,父类的public成员和protected成员继承到子类中成为private成员和private成员。父类中的private成员在子类中不可访问。
注意虽然父类中的private成员不可访问,但它们确实是继承到子类中的,可用sizeof();函数来验证。而且虽然继承的父类中的private成员不可以直接访问,但如果继承的父类
中的public或protected函数可以访问private成员,那么子类中也可以通过该方式间接访问private成员。
2.继承的语法
继承的说明在class后,“{”前。不同的父类由逗号隔开如
class father1{ ...}; class father2{...};
class son:public father1,protected father2
{...
};


0 0
原创粉丝点击