C++中继承(inheritance)详解及其作用介绍

概述 面向对象程序设计中最重要的一个概念是继承 (inheritance). 继承允许我们依据另一个类来定义一个类, 这使得创建和维护一个应用程序变得更统一. 这样做也达到了重用代码功能和提高执行效率的效果.
C++中继承(inheritance)详解及其作用介绍
文章图片

类的概念 一个类中包含了若干数据成员和成员函数. 不同的类中的数据成员和成员函数各不相同. 但是有时两个类的内容基本相同. 例如:
C++中继承(inheritance)详解及其作用介绍
文章图片

继承的概念 继承 (inheritance) 就是在一个已存在的类的基础上建立一个新的类.

  • 已存在的类: 基类 (base class) 或父类 (father class)
  • 新建立的类: 派生类 (derived class) 或子类 (son class)
C++中继承(inheritance)详解及其作用介绍
文章图片

一个新类从已有的类获得其已有特性, 称为类的继承.
  • 通过继承, 一个新建的子类从已有的父类那里获得父类的特性
  • 派生类继承了基类的所有数据成员和成员函数, 并可以对成员做必要的增加或调整
从已有的类 (父类) 产生一个新的子类, 称为类的派生.
  • 类的继承是用已有的类来建立专用新类的编程技术
  • 一个基类可以派生出多个派生类, 每一个派生类又可以作为基类再派生出新的派生类. 因此基类和派生类是相对而言的
  • 派生类是基类的具体化, 而基类则是派生类的抽象
单继承
单继承 (single inheritance) 指一个派生类只从一个基类派生.
  • 单继承关系形成的层次是一个树形结构
  • 箭头表示继承的方向, 从派生类指向基类
C++中继承(inheritance)详解及其作用介绍
文章图片

多重继承
多重继承 (multiple inheritance) 是指一个派生类有两个或多个基类. 派生类不仅可以从一个基类派生, 也可以从多个基类派生.
C++中继承(inheritance)详解及其作用介绍
文章图片

派生类 派生类的声明方式:
class 派生类名:[继承方式]基类名{派生类新增加的成员};

成员访问限定符 (默认 private):
  • public (公用的)
  • private (私有的)
  • protected (受保护的)
继承方式包括 (默认 private):
  • public (公用的)
  • private (私有的)
  • protected (受保护的)
Student 类:
#ifndef PROJECT5_STUDENT_H#define PROJECT5_STUDENT_H#include using namespace std; class Student {protected:int number; string name; char sex; public:Student(int num, string n, char s); void show(); }; #endif //PROJECT5_STUDENT_H

Student.cpp:
#include #include "Student.h"using namespace std; Student::Student(int num, string n, char s) {number = num; name = n; sex = s; }void Student::show() {cout << "number: " << number << endl; cout << "name: " << name << endl; cout << "sex: " << sex << endl; }

Student 派生类:
#ifndef PROJECT5_STUDENT1_H#define PROJECT5_STUDENT1_H#include "Student.h"class Student1:public Student {private:int age; string address; public:Student1(int num, string n, char s, int a, string addr); void show1(); }; #endif //PROJECT5_STUDENT1_H

Student1.cpp:
#include #include "Student1.h"using namespace std; Student1::Student1(int num, string n, char s, int a, string addr) : Student(num, n, s) {Student(num, n, s); age = a; address = addr; }void Student1::show1() {show(); cout << "age: " << age << endl; cout << "address: " << address << endl; }

【C++中继承(inheritance)详解及其作用介绍】mian:
#include #include "Student1.h"int main() {Student1 student1(1, "Little White", 'f', 18, "火星"); student1.show1(); return 0; }

输出结果:
number: 1name: Little Whitesex: fage: 18address: 火星

派生类中的成员包括从基类继承过来的成员和自己增加的成员两大部分. 每一部分布分别包括数据成员和成员函数.
派生类的构造函数和析构函数
构造函数和析构函数:
构造函数的主要作用是对数据成员初始化析构函数在释放对象前做一些相关的处理
因为派生类还继承了基类的数据成员. 设计派生类的构造函数时, 不仅要考虑派生类所增加的数据成员的初始化, 还应当考虑基类的数据成员初始化. 于是我们在执行派生类的构造函数时, 调用基类的构造函数.
派生类构造函数一般形式
派生类构造函数名 (总形式参数表列) : 基类构造函数名 (实际参数表列) {派生类中新增数据成员初始化语句}

类内定义 在类内定义派生类构造函数:
Student1::Student1(int num, string n, char s, int a, string addr) : Student(num, n, s), age(a), address(addr) {}

类外定义 在类的外面定义派生类构造函数:
类内:Student1(int num, string n, char s, int a, string addr); 类外:Student1::Student1(int num, string n, char s, int a, string addr) : Student(num, n, s) {Student(num, n, s); // 基类age = a; address = addr; }

构造函数和析构函数执行的顺序
建立派生类对象时, 执行构造函数的顺序:
  • 派生类构造函数先调用基类构造函数
  • 再执行派生类构造函数本身 (即派生类构造函数的函数体)
在派生类对象释放时:
  • 先执行派生类析构函数 ~Derived()
  • 再执行其基类析构函数 ~Base()
Base 类:
#ifndef PROJECT5_BASE_H#define PROJECT5_BASE_Hclass Base {protected:Base(); ~Base(); }; #endif //PROJECT5_BASE_H

Base.cpp:
#include #include "Base.h"using namespace std; Base::Base() {cout << "基类构造" << endl; }Base::~Base() {cout << "基类析构" << endl; }

Derived 类:
#ifndef PROJECT5_DERIVED_H#define PROJECT5_DERIVED_H#include "Base.h"using namespace std; class Derived: public Base{public:Derived(char c); ~Derived(); }; #endif //PROJECT5_DERIVED_H

Derived.cpp:
#include #include "Derived.h"using namespace std; Derived::Derived(char c) {cout << "子类构造函数, 值:" << c << endl; }Derived::~Derived() {cout << "子类析构函数" << endl; }

main:
#include #include "Derived.h"using namespace std; Derived::Derived(char c) {cout << "子类构造函数, 值:" << c << endl; }Derived::~Derived() {cout << "子类析构函数" << endl; }

输出结果:
基类构造子类构造函数, 值:b子类析构函数基类析构

子对象派生 子对象 (sub object), 即对象中的对象. 类的数据成员是另一个类的对象.
Student1 类:
#ifndef PROJECT5_STUDENT1_H#define PROJECT5_STUDENT1_H#include "Student.h"class Student1:public Student {private:int age; string address; Student president; public:Student1(int num, string n, char s, int p_num, string p_n, char p_s, int a, string addr); void show1(); }; #endif //PROJECT5_STUDENT1_H

Student1.cpp:
#include #include "Student1.h"using namespace std; Student1::Student1(int num, string n, char s, int p_num, string p_n, char p_s, int a, string addr) : Student(num, n, s), president(p_num, p_n, p_s) {age = a; address = addr; }void Student1::show1() {show(); cout << "age: " << age << endl; cout << "address: " << address << endl; cout << "==========班长信息==========" << endl; president.show(); }

main:
#include #include "Student1.h"using namespace std; int main() {Student1 student1(1, "Little White", 'f', 2, "班长", 'm', 18, "火星"); student1.show1(); return 0; }

输出结果:
number: 1name: Little Whitesex: fage: 18address: 火星==========班长信息==========number: 2name: 班长sex: m

注意事项
  • 当不需要对派生类新增的成员函数进行任何初始化操作时, 派生类构造函数体可以为空
  • 基类没有构造函数或构造函数参数为空, 在派生类构造函数中可不写调用基类构造函数的语句, 盗用派生类构造函数时系统会自动调用基类的默认构造函数
  • 基类中定义了有参的构造函数, 派生类构造函数总必须写出基类的构造函数及其参数
  • 基类中既定义无参数的构造函数,又重载了有参数的构造函数, 派生类构造函数中可以调用带参的基类构造函数, 也可以不调用基类的构造函数
到此这篇关于C++中继承(inheritance)详解及其作用介绍的文章就介绍到这了,更多相关C++继承内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读