目录
(一)派生类的构造函数必须调用基类的构造函数初始化基类的那一部分成员。如果基类没有默认的构造函数,则必须在派生类构造函数的初始化列表阶段显示调用
(二)派生类的拷贝构造函数必须调用基类的拷贝构造完成基类的拷贝初始化
(三)派生类的operator=必须要调用基类的operator=完成基类的复制
(四)派生类的析构函数会在被调用完成后自动调用基类的析构函数清理基类成员。因为这样才能保证派生类对象先清理派生类成员再清理基类成员的顺序
- #include
- 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;
- }
- #include
- 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
- 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
- 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
- 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;
- }
- #include
- 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;
- }
- #include
- 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;
- }
-
- #include
- 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;//职工编号
- };
- 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;
- };
- #include
- 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;
- }