目录
当研究生老师与学生时,可以用Student类、Teacher类记录信息,但是:名字、年龄……肯定都是一样的。所以,如果在Student类、Teacher类中都列一遍成员变量,这就会重复,难免是对空间的浪费。
于是,写三个类:Postgraduate类、Student类、Teacher类。利用继承的方式在Postgraduate类中写共有的成员变量。
继承的意义:是类设计层次的复用。
继承的定义格式如下:

- class Postgraduate {
- public:
- int _age = 0; //年龄
- };
-
- class Student : public Postgraduate {
- public:
- int _stuid = 0; //学号
- };
-
- class Teacher : public Postgraduate {
- public:
- int _teaid = 0; //工号
- };

继承后,父类Postgraduate的成员,包括成员函数和成员变量,都会变成子类的一部分。
|
类成员/继承方式
| public继承 | protected继承 | private继承 |
|---|---|---|---|
| 基类的public成员 | 派生类的public成员 |
派生类的protected成员
|
派生类的private成员
|
|
基类的protected成员
|
派生类的protected成员
|
派生类的protected成员
|
派生类的private成员
|
|
基类的private成员
| 在派生类中不可见 | 在派生类中不可见 |
在派生类中不可见
|
Note:在实际运用中一般使用都是public继承,几乎很少使用protetced/private继承,也不提倡使用protetced/private继承,因为protetced/private继承下来的成员都只能在派生类的类里面使用,实际中扩展维护性不强。
- #include
- using namespace std;
- class A {
- protected:
- int _a;
- };
-
- class B : public A {
- void test() { _a = 1; } //correct
- };
-
- int main() {
- B b1;
- b1._a; //error 不可见 -> 派生类外不能访问
- return 0;
- }
3. 基类private成员在派生类中无论以什么方式继承都是不可见的。
Note:不可见:B类继承了A类,B类复用A类中的成员。虽然B中的成员对A中的所有成员复用了,但是,对于基类的private成员,无论以什么方式继承到派生类对象中,都是不可见的。即,语法上限制派生类对象不管在类里面还是类外面都不能去访问它。
- #include
- using namespace std;
- class A {
- private:
- int _a;
- };
-
- class B : public A {
- void test() { _a = 1; } //error 不可见 -> 派生类里不能访问
- };
-
- int main() {
- B b1;
- b1._a; //error 不可见 -> 派生类外不能访问
- return 0;
- }
- #include
- using namespace std;
- class A {
- public:
- int _a;
- };
-
- class B1 : A { }; //默认的继承方式是private
- struct B2 : A { }; //默认的继承方式是public
-
- int main() {
- B1 b1;
- B2 b2;
- b1._a = 1; //error -(根据表)-> 派生类的private成员
- b2._a = 1; //correct -(根据表)-> 派生类的public成员
- return 0;
- }
派生类对象 可以赋值给 基类的对象 / 基类的指针 / 基类的引用。这里有个形象的说法叫切片或者切割。寓意把派生类中父类那部分切来赋值过去。
- class Postgraduate {
- public:
- int _age = 0; //年龄
- };
-
- class Student : public Postgraduate {
- public:
- int _stuid = 0; //学号
- };
-
- void test() {
- Student s;
- //子类对象可以赋值给父类对象/指针/引用
- Postgraduate pobj = s;
- Postgraduate* pp = &s;
- Postgraduate& rp = s;
- }



Note:
基类对象不能赋值给派生类对象。基类的指针可以通过强制类型转换赋值给派生类的指针,但是此时基类的指针必须是指向派生类的对象才是安全的。
- class Postgraduate {
- public:
- int _age = 0; //年龄
- };
-
- class Student : public Postgraduate {
- public:
- int _stuid = 0; //学号
- };
-
- void test() {
- Postgraduate p;
- //基类对象不能赋值给派生类对象
- Student s = p; //error
- }
- class Postgraduate {
- public:
- int _age = 0; //年龄
- };
-
- class Student : public Postgraduate {
- public:
- int _stuid = 0; //学号
- };
-
- void test() {
- Student s;
- Postgraduate* pp = &s;
- Postgraduate& rp = s;
-
- Student* ps = (Student*)&pp;
- Student& rs = (Student&)rp;
- }
在继承体系中基类和派生类都有独立的作用域。如果:子类和父类中有同名成员,子类成员将屏蔽父类对同名成员的直接访问,这种情况叫隐藏,也叫重定义。(在子类成员函数中,可以使用基类::基类成员显示访问)

Note:
如果是成员函数的隐藏,只需要函数名相同就构成隐藏。 所以,注意在实际中在继承体系里面最好不要定义同名的成员。
- // Postgraduate中的Func和Student中的Func不是构成重载,因为不是在同一作用域。
- // Postgraduate中的Func和Student中的Func构成隐藏,成员函数满足函数名相同就构成隐藏。
- class Postgraduate {
- public:
- void Func() { cout << _name << endl; }
- int _name = 10;
- };
-
- class Student : public Postgraduate {
- public:
- int _name = 100;
-
- void Func() { cout << _name << endl; }
- void Func_s() { cout << _name << endl; }
- void Func_p1() { cout << Postgraduate::_name << endl; }
- };
-
- int main() {
- Student s;
- s.Postgraduate::Func(); //输出10
- s.Func_s(); //输出100
- s.Func_p1(); //输出10
-
- cout << s._name << endl; //输出100
- cout << s.Postgraduate::_name << endl; //输出10
- return 0;
- }
Note:(为什么不是形成函数重载?)
继承后,父类Postgraduate的成员,包括成员函数和成员变量,都会变成子类的一部分。但是,父类中的 Func函数 和子类中的 Func函数 并不会构成函数重载,因为函数重载要求两个函数在同一作用域,而此时这两个 Func函数 并不在同一作用域。

首先,我们须知继承是在栈中实例化。在栈上就要符合栈的特性:


- class Postgraduate {
- public:
- int _age; //年龄
- //没有默认构造函数
- Postgraduate(int n) {
- cout << "Postgraduate" << endl;
- }
- };
- class Student : public Postgraduate {
- public:
- int _stuid = 0; //学号
- Student()
- :Postgraduate(0) //必须在派生类构造函数的初始化列表显示调用
- {
- cout << "Student" << endl;
- }
- };
- int main(){
- Student s;
- return 0;
- }

- class Postgraduate {
- public:
- int _age = 0; //年龄
- ~Postgraduate() {
- cout << "~Postgraduate()" << endl;
- }
- };
- class Student : public Postgraduate {
- public:
- int _stuid = 0; //学号
- ~Student() {
- Postgraduate::~Postgraduate();
- }
- };
- int main(){
- Student s;
- return 0;
- }
派生类对象析构函数会在结束时自行调用基类析构函数,所以会有两个基类析构函数的调用。


派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化。如果基类没有默认的拷贝构造函数,则必须在派生类拷贝构造函数的初始化列表阶段显示调用。
- class Postgraduate {
- public:
- int _age = 0; //年龄
- //拷贝构造函数
- Postgraduate(const Postgraduate& p)
- :_age(p._age)
- {
- cout << "Person(const Person& p)" << endl; //验证是否调用
- }
- };
- class Student : public Postgraduate {
- public:
- int _stuid = 0; //学号
- //拷贝构造函数
- Student(const Student& p)
- :Postgraduate(p)
- ,_stuid(p._stuid)
- {
- cout << "Student(const Student& p)" << endl; //验证是否调用
- }
- };

派生类的operator=必须要调用基类的operator=完成基类的复制。
- class Postgraduate {
- public:
- int _age = 0; //年龄
- //赋值运算符重载函数
- Postgraduate& operator=(const Postgraduate& p)
- {
- cout << "Postgraduate::operator=" << endl;//验证是否调用
- _age = p._age;//完成基类成员的赋值
- return *this;
- }
- };
- class Student : public Postgraduate {
- public:
- int _stuid = 0; //学号
-
- //赋值运算符重载函数
- Student& operator=(const Student& s)
- {
- cout << "Student::operator=" << endl; //验证是否调用
- Postgraduate::operator=(s); //调用基类的operator=完成基类成员的赋值
- _stuid = s._stuid; //完成派生类成员的赋值
- return *this;
- }
- };
友元关系不能继承,也就是基类的友元可以访问基类的私有和保护成员,而基类友元不能访问派生类的私有和保护成员。
- #include
- #include
- using namespace std;
-
- class Student;//声明有Student类
-
- class Postgraduate{
- public:
- friend void Display(const Postgraduate& p, const Student& s);
- protected:
- int _age = 0; //年龄
- };
-
- class Student : public Postgraduate {
- protected:
- int _stuid = 0; //学号
- };
-
- void Display(const Postgraduate& p, const Student& s){
- cout << p._age << endl; //输出0
- cout << s._stuid << endl; //error - 无法访问protected修饰的对象
- }
-
- int main(){
- Postgraduate p;
- Student s;
- Display(p, s);
- return 0;
- }
若想基类的友元函数访问派生类的私有和保护成员,需要在派生类中对该函数也进行友元声明。
- class Student : public Postgraduate {
- friend void Display(const Postgraduate& p, const Student& s);
- protected:
- int _stuid = 0; //学号
- };
- #include
- #include
- using namespace std;
-
- class Person
- {
- public:
- Person() { ++_count; }
- static int _count; // 统计人的个数。
- };
- int Person::_count = 0;
- class Student : public Person { /* …… */ };
- class Graduate : public Student { /* …… */ };
-
- int main() {
- Student s1;
- Student s2;
- Student s3;
- Graduate s4;
- cout << " 人数 :" << Person::_count << endl; //输出: 人数 :4
- Student::_count = 0;
- cout << " 人数 :" << Person::_count << endl; //输出: 人数: 0
-
- return 0;
- }
单继承:子类只有一个直接父类时称这个继承关系为单继承

多继承:一个子类有两个或以上直接父类时称这个继承关系为多继承

菱形继承:菱形继承是多继承的一种特殊情况。

菱形继承的问题:菱形继承会导致数据冗余和二义性的问题。(下面是对象成员模型构造的菱形继承)
- class person {
- public:
- int age = 1; //年龄
- };
-
- class Student : public person {
- public:
- int _stuid = 2; //学号
- };
-
- class Teacher : public person {
- public:
- int _teaid = 3; //工号
- };
-
- class Postgraduate : public Student, public Teacher {
- public:
- int a = 4;
- };
-
- int main() {
- Postgraduate p;
- return 0;
- }
- class person {
- public:
- int age = 1; //年龄
- };
-
- class Student : virtual public person {
- public:
- int _stuid = 2; //学号
- };
-
- class Teacher : virtual public person {
- public:
- int _teaid = 3; //工号
- };
-
- class Postgraduate : public Student, public Teacher {
- public:
- int a = 4;
- };
-
- int main() {
- Postgraduate p;
- return 0;
- }

虚基表中包含三个数据:
第一个数据:是为多态的虚表预留的存偏移量的位置(多态再涉及,此处可以不管)。
第二个数据:是当前类对象位置距离公共虚基类的偏移量。
第二个数据:VS编译器的设定,最后一个地址为0x00 00 00 00。

在Student类或Theacher类范围中访问时:这两个指针经过一系列的计算,最终都可以找到成员。
C++语言之所以复杂,继承就是其中的原因之一,而继承之所以复杂就是因为存在菱形继承,有了菱形继承就有菱形虚拟继承,底层实现就很复杂。所以一般不建议设计出多继承,一定不要设计出菱形继承。否则在复杂度及性能上都有问题。(多继承可以认为是C++的缺陷之一,所以后来的编程语言很多都没有多继承,如Java。 )