本文摘自PHP中文网,作者php是最好的语言,侵删。
继承方式和访问限定符的关系
三种继承方式:
公有继承
私有继承
保护继承
关系:若基类的成员函数为私有,则派生类对基类的私有成员是不可见的,其他的标准为选范围小的为最终访问限定。
保护成员限定符:一些基类成员不想被基类的对象直接访问,但需要在派生类中才能访问,就定义为保护成员。保护成员限定符是因继承才出现的。
理解隐藏
隐藏是指派生类的函数屏蔽了与其同名的基类函数。规则如下:
如果派生类的函数与基类的函数同名,但是参数不同,此时,不论有无virtual关键字,基类的函数将被隐藏。
如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual关键字,此时,基类的函数被隐藏。
派生类的默认成员函数
在派生类中如果没有显示定义一下六个默认成员函数,编译系统会默认合成这六个成员函数。
构造函数
拷贝构造函数
析构函数
赋值操作符重载
取地址操作符重载
const修饰的取地址操作符重载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include<iostream>
using namespace std;
class person{
public:
person(const char* name)
:_name(name)
{
cout<< "person()" <<endl;
}
person(const person& p){
cout<< "person(const person& p)" <<endl;
}
person& operator=(const person& p){
cout<< "person& operator=(const person& p)" <<endl;
if(this!=&p){
_name=p._name;
}
return *this;
}
~person(){
cout<< "~person()" <<endl;
}
protected:
string _name;
};
class student:public person{
public:
student(const char* name,int num)
:person(name)
,_num(num){
cout<< "student()" <<endl;
}
student(const student& s)
:person(s)
,_num(s._num)
{
cout<< "student(const student& s)" <<endl;
}
student& operator=(const student& s){
cout<< "student& operator=(const student& p)" <<endl;
if(this!=&s){
person::operator=(s);//必须指定域,否则会死循环
_num=s._num;
}
return *this;
}
~student(){//析构的时候先清理子类,再清理父类,不需要显示的调用
cout<< "~student()" <<endl;
}
private:
int _num;
};
int main(){
student s 1 ( "jack" , 18 );
student s 2 (s 1 );
student s 3 ( "rose" , 16 );
s 1 =s 3 ;
}
|
菱形继承

菱形继承存在二义性以及数据冗余问题。
例,下图中继承的数据为两份,各不相同:

解决方法:虚继承
在菱形的第二层添加virtual
例:

相关文章:
C#拾遗之小知识(四):继承
C#基础知识整理:基础知识(2) 类
相关视频:
千锋C语言基础视频教程
以上就是【C++】深入了解继承方式基础知识及其与访问限定符的关系的详细内容!
相关阅读 >>
c++如何比较两个字符串?
c++万能头文件是什么?
c++ 引用和指针区别
c++源程序文件的扩展名是什么
第五章c++:语句的相关介绍
c++与c语言的区别与联系
c++ vector用法是什么
浅谈c++生成guid的两种方法
c++运算符中不能重载的是哪些
c++中new的用法详解
更多相关阅读请进入《继承方式》频道 >>
转载请注明出处:木庄网络博客 » 【C++】深入了解继承方式基础知识及其与访问限定符的关系