目录
(一)派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用
(二)派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化
(三)派生类的operator=必须要调用基类的operator=完成基类的复制
(四)派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类对象先清理派生类成员再清理基类成员的顺序
一、继承的概念
- 继承(inheritance)机制是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称派生类。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。以前我们接触的复用都是函数复用,继承是类设计层次的复用
#include <iostream>
using namespace std;
class Person
{
public:
void Print()
{
cout << "name:" << _name << endl;
cout << "age:" << _age << endl;
}
protected:
string _name = "peter"; // 姓名
int _age = 18; // 年龄
};
// 继承后父类的Person的成员(成员函数+成员变量)都会变成子类的一部分。
// 这里体现出了Student和Teacher复用了Person的成员。下面我们使用监视窗口查看Student和Teacher对象,可
//以看到变量的复用。调用Print可以看到成员函数的复用。
class Student : public Person
{
protected:
int _stuid; // 学号
};
class Teacher : public Person
{
protected:
int _jobid; // 工号
};
int main()
{
Student s;
Teacher t;
s.Print();
t.Print();
return 0;
}
二、基类和派生类对象赋值转换
- 派生类对象 可以赋值给 基类的对象 / 基类的指针 / 基类的引用。这里有个形象的说法叫切片或者切割。寓意把派生类中父类那部分切来赋值过去。
- 基类对象不能赋值给派生类对象。
- 基类的指针或者引用可以通过强制类型转换赋值给派生类的指针或者引用。但是必须是基类的指针是指向派生类对象时才是安全的。这里基类如果是多态类型,可以使用RTTI(Run-
- Time Type Information)的dynamic_cast 来进行识别后进行安全转换。(ps:这个我们后面再讲解,这里先了解一下)
三、继承中的作用域
- 在继承体系中基类和派生类都有独立的作用域。
- 子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏,也叫重定义。(在子类成员函数中,可以使用 基类::基类成员 显示访问)
- 需要注意的是如果是成员函数的隐藏,只需要函数名相同就构成隐藏。
- 注意在实际中在继承体系里面最好不要定义同名的成员
#include <iostream>
using namespace std;
class Person
{
protected:
string _name = "peter"; // 姓名
int _num = 100; // 身份证号
};
class Student : public Person
{
public:
void Print()
{
cout << "姓名:" << _name << endl;
cout << "身份证号:" << _num;
}
protected:
int _num = 999; // 身份证号
};
int main()
{
Student s;
s.Print();
return 0;
}
#include <iostream>
using namespace std;
// B中的fun和A中的fun不是构成重载,因为不是在同一作用域
// B中的fun和A中的fun构成隐藏,成员函数满足函数名相同就构成隐藏。
class A
{
public:
void fun()
{
cout << "func()" << endl;
}
};
class B : public A
{
public:
void fun(int i)
{
cout << "func(int i)->" << i << endl;
}
};
int main()
{
B b;
b.fun(10);
b.A::fun();//指定作用域
return 0;
}
四、派生类的默认成员函数
6个默认成员函数,在派生类中,这几个成员函数是如何生成的呢?
(一)派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用
- 派生类对象初始化先调用基类构造再调派生类构造
#include <iostream>
using namespace std;
class Person
{
public:
Person(const char* name = "peter")
: _name(name)
{
cout << "Person()" << endl;
}
protected:
string _name; // 姓名
};
class Student : public Person
{
public:
Student(const char* name, int id)
: Person(name)
,_id(id)
{
cout << "Student(const char* name, int id)" << endl;
}
protected:
int _id;
};
int main()
{
Student s1("张三", 18);
return 0;
}
(二)派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化
#include <iostream>
using namespace std;
class Person
{
public:
Person(const char* name = "peter")//构造函数
: _name(name)
{
cout << "Person()" << endl;
}
Person(const Person& p)//拷贝构造函数
: _name(p._name)
{
cout << "Person(const Person& p)" << endl;
}
protected:
string _name; // 姓名
};
class Student : public Person
{
public:
Student(const char* name, int id)//构造函数
: Person(name)
,_id(id)
{
cout << "Student(const char* name, int id)" << endl;
}
Student(const Student& s)//拷贝构造函数
:Person(s)
, _id(s._id)
{
cout << "Student(const Student& s)" << endl;
}
protected:
int _id;
};
int main()
{
Student s1("张三", 18);
Student s2(s1);
return 0;
}
(三)派生类的operator=必须要调用基类的operator=完成基类的复制
(四)派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类对象先清理派生类成员再清理基类成员的顺序
- 派生类对象析构清理先调用派生类析构再调基类的析构
#include <iostream>
using namespace std;
class Person
{
public:
Person(const char* name = "peter")//构造函数
: _name(name)
{
cout << "Person()" << endl;
}
~Person()
{
cout << "~Person()" << endl;
}
protected:
string _name; // 姓名
};
class Student : public Person
{
public:
Student(const char* name, int id)//构造函数
: Person(name)
,_id(id)
{
cout << "Student(const char* name, int id)" << endl;
}
~Student()
{
cout << "~Student()" << endl;
}
protected:
int _id;
};
int main()
{
Student s1("张三", 18);
return 0;
}
五、继承与友元、静态成员
- 友元关系不能继承,也就是说基类友元不能访问子类私有和保护成员
- 基类定义了static静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子
类,都只有一个static成员实例
#include <iostream>
using namespace std;
class Person
{
public:
Person()
{
++_count;
}
protected:
string _name; // 姓名
public:
static int _count; // 统计人的个数。
};
int Person::_count = 0;
class Student : public Person
{
protected:
int _stuNum; // 学号
};
int main()
{
cout << &Person::_count << endl;
cout << &Student::_count << endl;
return 0;
}
六、复杂的菱形继承及菱形虚拟继承
- 单继承:一个子类只有一个直接父类时称这个继承关系为单继承
- 多继承:一个子类有两个或以上直接父类时称这个继承关系为多继承
- 菱形继承:菱形继承是多继承的一种特殊情况
(一)菱形继承的问题
- 菱形继承的问题:从下面的对象成员模型构造,可以看出菱形继承有数据冗余和二义性的问题。
- 在Assistant的对象中Person成员有两份
#include<iostream>
using namespace std;
class Person
{
public:
string _name;//姓名
};
class Student :public Person
{
protected:
int _num;//学号
};
class Teacher :public Person
{
protected:
int _id;//职工编号
};
class Assistant :public Student, public Teacher//先继承Student,再继承Teacher
{
protected:
string _majorCourse; // 主修课程
};
int main()
{
Assistant a;
//a._name = "李明";//错误,不知道访问哪个类中的_name
//正确,显示指定访问哪个父类的成员
a.Student::_name = "张三";
a.Teacher::_name = "李四";
return 0;
}
(二)解决菱形继承的二义性和数据冗余
采用虚拟继承。如上面的继承关系,在Student和Teacher的继承Person时使用虚拟继承,即可解决问题。需要注意的是,虚拟继承不要在其他地方去使用。
class Student :virtual public Person
{
protected:
int _num;//学号
};
class Teacher :virtual public Person
{
protected:
int _id;//职工编号
};
(三)虚拟继承解决数据冗余和二义性的原理
1. 不采用虚拟继承的时候
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//先继承B,再继承C
{
public:
int _d;
};
2. 采用虚拟继承的时候
#include<iostream>
using namespace std;
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//先继承B,再继承C
{
public:
int _d;
};
int main()
{
D d;
d.B::_a = 1;
d._b = 3;
d.C::_a = 2;
d._c = 4;
d._d = 5;
return 0;
}