问题引入:
面向对象编程的主要目的之一是提供可重用的代码,C++提供了一个方法来扩展和修改类。这种方法叫做类继承。它能够从已有的类派生出新的类,而派生类继承了原有类(称为基类)的特征,包括方法。
目录
一个简单的基类:
从一个类派生出另一个类时,原始类称为基类,继承类称为派生类。为说明继承,首先需要一个基类。Person类中有_name,_age成员变量,有Print()成员方法。接下来我们将派生一个Student学生类
- //父类、基类
- class Person
- {
- public:
- void Print()
- {
- cout << "name :" << _name << endl;
- cout << "age :" << _age << endl;
- }
-
- //private:
- //protected:
- string _name = "Peter";
- int _age = 18;
- };
派生一个类:
派生类的声明方式,首先将Student类声明为从Person类派生而来:
class Student : public Person
冒号指出Student类的基类是Person类。public表示Person是一个公有基类,这被称为公有派生。派生类对象包含基类对象。使用公有派生,基类的公共成员将成为派生类的公有成员;基类的私有部分也将成为派生类的一部分,但只能通过基类的公有和保护方法访问。换句话说,Student类定义的对象s,可以使用Person类中的公有方法。
那么Student对象具有以下特征:
1.派生类对象存储了基类的数据成员(派生类继承了基类的实现)。
2.派生类对象可以使用基类的方法(派生类继承了基类的接口)。
派生类需要添加什么呢?
1. 派生类需要自己的构造函数。
2.派生类可以根据需要添加额外的数据成员和成员函数。
那么我们现在写一个Student类:
- class Student : public Person
- {
- public:
- void func()
- {
- Print();
- }
- protected:
- int _stuid;//学号
- };
我们可以看到Student类继承了Person类,并且是公有继承,并且Student类拥有自己额外的成员变量以及成员函数。

上文我们所提到的,Person类是基类(父类),Stundent是派生类(子类)。继承方式是公有继承。

继承方式不仅有我们刚提到的公有继承(public),还有保护继承(protected)以及私有继承(private),在类中,访问限定符可以选择性的将其接口提供给外部的用户使用。因此类内有3中方式,类继承有3中方式,这样就组成了9中的访问关系。

|
类成员
/
继承方式
|
public
继承
|
protected
继承
|
private
继承
|
|
基类的
public
成员
|
派生类的
public
成员
|
派生类的
protected
成员
|
派生类的
private
成员
|
|
基类的
protected
成员
|
派生类的
protected成员
|
派生类的
protected
成员
|
派生类的
private
成员
|
|
基类的
private
成
员
|
在派生类中
不可见
|
在派生类中
不可见
|
在派生类中
不可见
|
总结:
1. 基类 private 成员在派生类中无论以什么方式继承都是不可见的。这里的 不可见是指基类的私 有成员还是被继承到了派生类对象中,但是语法上限制派生类对象不管在类里面还是类外面 都不能去访问它 。2. 基类 private 成员在派生类中是不能被访问,如果基类成员不想在类外直接被访问,但需要在 派生类中能访问,就定义为 protected 。 可以看出保护成员限定符是因继承才出现的 。3. 实际上面的表格我们进行一下总结会发现,基类的私有成员在子类都是不可见。基类的其他成员在子类的访问方式 == Min(成员在基类的访问限定符,继承方式),public > protected
> private 。4.使用关键字class时默认的继承方式是private,使用struct时默认的继承方式是public,不过
最好显示的写出继承方式 。5.在实际运用中一般使用都是public继承,几乎很少使用protetced/private继承,也不提倡
使用 protetced/private 继承,因为 protetced/private 继承下来的成员都只能在派生类的类里面使用,实际中扩展维护性不强。
代码示例:
- class Person
- {
- public:
- void Print()
- {
- cout << "name:" << _name << endl;
- cout << "age:" << _age << endl;
- cout << "ID:" << _ID << endl;
- }
- public:
- string _name = "张三";
-
- protected:
- int _age = 18;
-
- private:
- int _ID = 1;
- };
-
- class Student : public Person
- {
-
- };



- class Person
- {
-
- //protected:
- public:
- string _name; // 姓名
- string _sex; // 性别
- int _age; // 年龄
- };
-
- class Student : public Person
- {
- public:
- int _No; // 学号
- };
1.子类对象给父类 对象/指针/引用 -- 语法天然支持,没有类型转换
- Person p;
- Student s;
- p = s; //对象
- Person& rp = s; //引用
- Person* ptrp = &s; //指针
2.基类对象不能赋值给派生类对象

3. 基类的指针可以通过强制类型转换赋值给派生类的指针
- ptrp = &s;
- Student* ps1 = (Student*)ptrp;//这种情况下转换是可以的
- ps1->_No = 10;
-
- ptrp = &p;
- Student* ps2 = (Student*)ptrp;//这种情况转换时虽然可以,但是会存在越界访问的问题
- ps2->_No = 10;
- class Person
- {
- protected:
- string _name = "张三"; // 姓名
- int _num = 111; // 身份证号
- };
-
- class Student : public Person
- {
- public:
- void Print()
- {
- cout << " 姓名:" << _name << endl;
- cout << " 学号:" << _num << endl;//会使用自己的成员变量 999
- cout << " 身份证号:" << Person::_num << endl; //要指定作用域
- }
- protected:
- int _num = 999; // 学号
- };
- int main()
- {
- Student s;
- s.Print();
- return 0;
- }
2. 成员方法导致隐藏关系
这段代码中b的fun()和A的fun()构成隐藏关系,b的fun()隐藏了A的fun(),若要使用A的fun()显示指定作用域即可访问到A的fun()。
- class A
- {
- public:
- void fun()
- {
- cout << "A::func()" << endl;
- }
- };
- class B : public A
- {
- public:
- void fun()
- {
- cout << "B::func()"<< endl;
- }
- };
-
- int main()
- {
- B b;
- b.fun();//b的fun()隐藏了A的fun()
- b.A::fun();//指定作用域即可访问到A的func()
- return 0;
- };
我们在学习类与对象第一节时就知道了类内会默认生成6个成员函数,那么在派生类中,这几个成员函数是如何生成的呢?
- class Person
- {
- public:
- Person(const char* name)
- :_name(name)
- {
- cout << "Person(const char* name)" << 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;//姓名
- };
-
- class Student : public Person
- {
- public:
- Student(const char* name = "", int num = 0)
- :_num(num)
- ,Person(name)
- {
- cout << "Student(const char* name = "", int num = 0)" << endl;
- }
-
- //拷贝构造
- Student(const Student& s)
- :Person(s)
- ,_num(s._num)
- {
- cout << "Student(const Student& s)" << endl;
- }
-
- //赋值构造
- // s1 = s3
- Student& operator=(const Student& s)
- {
- if (this != &s)
- {
- Person::operator=(s);
- _num = s._num;
- }
- cout << "Student& operator=(const Student& s)" << endl;
-
- return *this;
- }
-
- //父类和子类析构函数构成隐藏关系
- //原因:多态的需要,析构函数名同意会被处理成destructor()
- //为了保证析构的顺序,先子后父
- //子类析构函数完成后会自动调用父类析构函数,所以不需要我们显示调用
- ~Student()
- {
- //父类的析构函数我们不需要显示调用
- //Person::~Person();
- cout << "~Student()" << endl;
- }
- protected:
- int _num; //学号
-
- };

- class Student;
- class Person
- {
- public:
- friend void Display(const Person& p, const Student& s);
- protected:
- string _name; // 姓名
- };
-
- class Student : public Person
- {
- friend void Display(const Person& p, const Student& s);
- protected:
- int _stuNum; // 学号
- };
-
- void Display(const Person& p, const Student& s)
- {
- cout << p._name << endl;
- //要想访问到Student中的保护成员,要提供友元函数
- cout << s._stuNum << endl;
- }
- 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; // 研究科目
- };
- //
- int main()
- {
- Student s1;
- Student s2;
- Student s3;
- Graduate s4;
- Person s;
-
- cout << " 人数 :" << Person::_count << endl;
- cout << " 人数 :" << Student::_count << endl;
- cout << " 人数 :" << s4._count << endl;
-
- //同一个地址
- cout << " 人数 :" << &Person::_count << endl;
- cout << " 人数 :" << &Student::_count << endl;
- cout << " 人数 :" << &s4._count << endl;
-
- return 0;
- }

结论: 基类定义了static静态成员,则整个继承体系里面只有一个这样的成员。无论派生出多少个子类,都只有一个static成员实例 。
单继承:一个子类只有一个直接父类

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

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

- class Person
- {
- public:
- string _name; // 姓名
- int _a[10000]; //会有多份_a
- };
- class Student : virtual public Person
- {
- protected:
- int _num; //学号
- };
- class Teacher : virtual public Person
- {
- protected:
- int _id; // 职工编号
- };
- class Assistant : public Student, public Teacher
- {
- protected:
- string _majorCourse; // 主修课程
- };

这样会有二义性无法明确知道访问的是哪一个,因此需要我们显示指定访问那个父类的成员可以解决二义性问题,但是数据冗余问题无法解决。
- class A
- {
- public:
- int _a;
- //static int _a;
- };
-
- //int A::_a = 0;
-
- //class B : public A
- class B : virtual public A
- {
- public:
- int _b;
- };
-
- //class C : public A
- class C : virtual public A
- {
- public:
- int _c;
- };
-
- class D : public B, public C
- {
- public:
- int _d;
- };
- // Car和BMW Car和Benz构成is-a的关系
- class Car {
- protected:
- string _colour = "白色"; // 颜色
- string _num = "陕ABIT00"; // 车牌号
- };
-
- class BMW : public Car {
- public:
- void Drive() { cout << "好开-操控" << endl; }
- };
-
- class Benz : public Car {
- public:
- void Drive() { cout << "好坐-舒适" << endl; }
- };
-
- // Tire和Car构成has-a的关系
-
- class Tire {
- protected:
- string _brand = "Michelin"; // 品牌
- size_t _size = 17; // 尺寸
- };
-
- class Car {
- protected:
- string _colour = "白色"; // 颜色
- string _num = "陕ABIT00"; // 车牌号
- Tire _t; // 轮胎
- };
(本篇完)