继承机制是面向对象程序设计使代码可以复用的最重要的手段,这个机制允许程序员在保持原有类特性的基础上进行扩展,增加功能,这样产生新的类,称为派生类。继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程。以前解除的都是函数复用,继承是类设计层次的复用。
代码演示如下
#include <iostream> #include <string> using namespace std; class Person { public: void Print() { cout << "name:" << _name << endl; cout << "age:" << _age << endl; } protected: string _name = "peter"; // 姓名 int _age = 18; }; class Student : public Person { protected: int _stuid; }; int main() { Person p; Student s; p.Print(); s.Print(); return 0; }
继承后父类的Person的成员(成员函数+成员变量)都会变成子类的一部分。这里体现出了Student复用了Person的成员。下面我们使用监视窗口查看Student对象,可以看到变量的复用。调用Print可以看到成员函数的复用。
成员变量的复用
成员函数的复用
Person是父类,也称作基类。Student是子类,也称作派生类。
1.2.2 继承关系和访问限定符
1.2.3 继承基类成员访问方式的变化
注意(重点)
#include <iostream> #include <string> using namespace std; class Person { public: // 缺省 string _name = "陈志浩"; // 姓名 string _sex = "男"; // 性别 int _age = 25; // 年龄 }; class Student : public Person { public: int _No = 2019124084; // 学号 }; void Test() { Person pobj; Student sobj; // 修改子类的成员变量方便演示 sobj._name = "chenzhiiao"; sobj._age = 23; } int main() { Test(); return 0; }
基类对象不能赋值给派生类对象
指向基类的指针可以指向子类,并且可以强制转换为子类指针,引用同理也是可以的,这里有点诡异,其实也可以这样理解:这里指针指向的地址是一样的,只不过类型限制了可以看哪些部分,所以再强制转换回子类指针才不会出错。
上面这些类型转换都必须是在公有继承的前提下的。
来上代码
#include <iostream> using namespace std; class A { public: void fun() { cout << "func()" << endl; } }; class B : public A { public: void fun(int i) { cout << "func(int i)->" << i << endl; } }; void Test() { // B中的fun和A中的fun不是构成重载,因为不是在同一作用域 // B中的fun和A中的fun构成隐藏,成员函数满足函数名相同就构成隐藏。 B b; b.fun(10); b.A::fun();//可以使用 基类::基类成员 显示访问 }; int main() { Test(); return 0; }
派生类的6个默认成员函数,默认就是我们不写但是会自己生成,那么这4(只关注 构造、拷贝构造、析构、赋值重载)个默认成员函数是如何生成的呢?
派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用,派生类对象初始化先调用基类构造再调派生类构造。。
#include <iostream> #include <string> #include <ostream> using namespace std; class Person { public: friend ostream& operator<<(ostream& out, const Person& p); Person(const char* name = "peter") : _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=(const Person& p)" << endl; if (this != &p) _name = p._name; return *this; } ~Person() { cout << "~Person()" << endl; } protected: string _name; // 姓名 }; ostream& operator<<(ostream& out, const Person& p) { out << "name : " << p._name << endl; return out; } class Student : public Person{ public: friend ostream& operator<<(ostream& out, const Student& s); Student(const char* name = "chenzhiao", int no = 2019) :Person(name) ,_no(no) { cout << "Student()" << endl; } ~Student() { cout << "~Student()" << endl; } protected: int _no; }; ostream& operator<<(ostream& out, const Student& s) { out << "name : " << s._name << ",no : " << s._no << endl; return out; } void Test() { Student s1; cout << s1; }; int main() { Test(); return 0; }
派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化,下面代码中的 Person(s) 有切片行为。
Student(const Student& s) : Person(s), _no(s._no) { cout << "Student(const Student& s)" << endl; }
派生类的operator=必须要调用基类的operator=完成基类的复制。
Student& operator=(const Student& s) { if (this != &s) { Person::operator=(s); //这里必须要用类名指定,否则子类的operator=会把父类的 //的operator=隐藏掉,导致无限递归调用子类的operator=导致栈溢出 _no = s._no; } return *this; }
派生类对象析构清理先调用派生类析构再调基类的析构,这里 c++ 的编译器就保证了先调用子类的析构再调用父类的析构。
有两个迷惑点:1、子类的析构函数和父类的析构函数构成 隐藏,这是因为多态重写的需要(下一篇文章我会拉出来再谈一下),所有类的析构函数,名字会被统一处理成 destructor 2 、 如果自己显示调用,存在父类会析构的问题,不符合先定义后析构的规则
~Student() { cout << "~Student()" << endl; Person::~Person(); //C++的编译器保证了这个代码默认会执行,但是必须要放在最后一行才符合规则 //不能让程序员去手动指定Person::~Person();必须要放在最后一行 //万一程序员不小心放在了第一行,会导致一些不可预料的错误,所以不要显示调用 //这和上面3个默认成员的规则不一样,需要多注意 }
最后补上一张图,有助于理解
友元关系不能继承,也即是说:基类友元不能访问子类私有和保护成员
基类定义了 static 静态成员,则整个继承体系里面只有一个这样的成员,无论派生出多少个子类,都只有一个 static 成员实例
来段代码
#include <iostream> #include <string> 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; // 学号 }; class Graduate : public Student { protected: string _seminarCourse; // 研究科目 }; void TestPerson() { Student s1; Student s2; Student s3; Graduate s4; Person s5; cout << " 人数 :" << Person::_count << endl; Student::_count = 0; cout << " 人数 :" << Person::_count << endl; } int main() { TestPerson(); return 0; }
一个子类只有一个直接父类,这种继承关系叫做单继承
一个子类有两个或者两个以上的父类叫做多继承
菱形继承是多继承的一种特殊情况
菱形继承存在很大的问题,从上面的对象模型图里可以得出:菱形继承有数据冗余和数据二义性的问题,在Assistant 的对象里 Person 成员有两份
#include <iostream> #include <string> using namespace std; class Person{ public: string _name; }; class Student : public Person{ protected: int _stuId; }; class Teacher : public Person{ protected: int _teaId; }; class Assistant : public Student, public Teacher{ protected: string _majorCourse; }; void Test() { Assistant a; //a._name = "chenzhiao"; 这样会有二义性无法明确知道访问的是哪一个 // 需要显式指定访问哪个父类的成员可以解决二义性的问题,但是数据冗余的问题还是无法解决 a.Student::_name = "xxx"; a.Teacher::_name = "yyy"; } int main() { Test(); return 0; }
虚拟继承可以解决菱形继承的二义性和数据冗余的问题。如上面的继承关系,在Student和Teacher的继承,Person时使用虚拟继承,即可解决问题。需要注意的是,虚拟继承不要在其他地方去使用,改进上面的代码。
#include <iostream> #include <string> using namespace std; class Person{ public: string _name; }; class Student : virtual public Person{ protected: int _stuId; }; class Teacher : virtual public Person{ protected: int _teaId; }; class Assistant : public Student, public Teacher{ protected: string _majorCourse; }; void Test() { Assistant a; a._name = "chenzhiao"; } int main() { Test(); return 0; } // 在菱形继承的“肩膀处”添加完 virtual 关键字,完成虚拟继承,可以解决数据的二义性和冗余问题。我们来看一下监视窗口
为了研究虚拟继承原理,我们给出了一个简化的菱形继承体系,再借助内存窗口观察对象成员的模型。
#include <iostream> #include <string> using namespace std; class A { public: int _a; }; // class B : public A class B : public A { public: int _b; }; // class C : public A class C : public A { public: int _c; }; class D : public B, public C { public: int _d; }; void Test() { D d; cout << sizeof(d) << "字节" << endl; d.B::_a = 1; d.C::_a = 2; d._b = 3; d._c = 4; d._d = 5; } int main() { Test(); return 0; }
下图是菱形继承的内存对象成员模型:这里可以看到数据冗余
下图是菱形虚拟继承的内存对象成员模型