C++中的继承
1 继承的概念、定义及继承规则
1.1 继承的概念
继承是面向对象程序设计使代码可以复用的重要手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称派生类。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。继承是类设计层次的复用。
1.2 继承的定义
class person
{
protected:
string _name;
};
class student : public person
{
private:
int _snum;
};
上述代码定义了一个student类继承了person类,下面我们看看继承的格式:
这里的继承方式又分为三种,分别是:public、protected、private
使用关键字class时默认的继承方式是private,使用struct时默认的继承方式是public,最好显示的写出继承方式。
继承方式决定了派生类对基类成员的访问权限,如下图:
注:
- 基类的友元函数无论是什么继承方式都不会被派生类继承
- 基类中的静态成员在整个继承体系中只存在一份
2 派生类与基类的对象赋值转换规则
- 派生类对象可以赋值给基类的对象、基类的引用、基类的指针
- 基类对象不能赋值给派生类
- 基类的指针可以通过强制类型转换赋值给派生类的指针。只有当这个基类指针指向派生类对象时才是安全的。
class person
{
protected:
string _name;
};
class student : public person
{
private:
int _snum;
};
3 派生类中同名隐藏问题
子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏,也叫重定义。(在子类成员函数中,可以使用 基类::基类成员 显示访问)
如果是成员函数的隐藏,只需要函数名相同就构成隐藏。
举例如下:
class person
{
public:
void func()
{
cout << "person::func" << endl;
}
protected:
int _id = 111; //这里是给缺省值,默认构造函数会用该缺省值进行初始化
string _name;
};
class student : public person
{
public:
void func() //隐藏了父类中的成员函数func()
{
cout << "student::func" << endl;
}
void print()
{
cout << "_id:" << _id << endl;
cout << "person::_id:" << person::_id << endl;
}
protected:
int _id = 999; //隐藏了父类中的成员变量_id
};
void test()
{
student s;
s.func();
s.person::func();
s.print();
}
运行结果如下图:
4 派生类与基类默认成员函数间的调用关系
- 派生类的构造函数必须先调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用。(派生类对象初始化时先调用基类构造函数,再调用派生类构造函数)
- 派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化。
- 派生类的operator=必须要调用基类的operator=完成基类的复制。
- 派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类对象先清理派生类成员再清理基类成员的顺序。(派生类对象析构清理内存时先调用派生类析构函数额,再调用基类析构函数)
举例如下:
class person
{
public:
person(const string& name="make")
:_name(name)
{
cout << "person()" << endl;
}
person(const person& p)
:_name(p._name)
{
cout << "person(const person& p)" << endl;
}
person& operator=(const person& p)
{
cout << "person operator=" << endl;
if (this != &p)
{
_name = p._name;
}
return *this;
}
~person()
{
cout << "~person()" << endl;
}
protected:
string _name;
};
class student : public person
{
public:
student(string name="tom", int num=18)
: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=" << endl;
if (this != &s)
{
person::operator=(s);
_num = s._num;
}
return *this;
}
~student()
{
cout << "~student()" << endl;
}
protected:
int _num;
};
void test1()
{
student s("张三", 23);
student s1(s);
student s2;
s2 = s1;
}
下图为上例中测试代码的运行结果及每个对象调用默认构造函数的顺序,验证了上述规则。
5 菱形继承和虚拟继承
5.1 什么是菱形继承?
C++中有多种继承方式:
- 单继承:一个子类只有一个直接父类
- 多继承:一个子类两个或以上直接父类
- 菱形继承:菱形继承是多继承的一种特殊情况,一个子类有两个或以上直接父类,这些父类又有相同的父类
5.2 菱形继承产生的问题
class A
{
public:
int _a;
};
class B : public A
{
public:
int _b;
};
class C : public A
{
public:
int _c;
};
class D : public B, public C
{
public:
int _d;
};
- 菱形继承会产生数据冗余:
在上述代码中,发生了菱形继承,B类和C类都继承了A,即B和C类中都有成员变量a,而D同时继承了B和C,D中就包含了两个成员变量a,因此产生了数据冗余。
void test2()
{
D d;
d.B::_a = 1;
d.C::_a = 2;
d._b = 3;
d._c = 4;
d._d = 5;
}
- 菱形继承中数据存在二义性:
由1可知,类D中包含了两个成员变量a,当直接访问成员变量a时,就存在二义性问题。
void test2()
{
D d;
d.a = 1;
}
5.3 解决菱形继承问题的方法——虚拟继承
在上述菱形继承关系中,如果在B和C继承A是使用虚拟继承,就可以避免上述问题。
虚拟继承定义格式:
class A
{
public:
int a;
};
class B : virtual public A
{
public:
int b;
};
class C : virtual public A
{
public:
int c;
};
class D : public B, public C
{
public:
int d;
};
可以简单地认为:菱形虚拟继承关系中,D中只存在一份成员变量a,所以不存在二义性和数据冗余,下面我们了解菱形虚拟继承的底层是如何解决这些问题的:
为什么要用虚基表指针、虚基表和偏移量要找a呢?
是为了解决派生类赋值转换给基类的问题:
D d;
B b = d;
C c = d;
只有这样设计,派生类赋值给基类时,基类才能准确的切片。