温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++中的继承的概念和功能介绍

发布时间:2021-09-07 07:27:55 来源:亿速云 阅读:109 作者:chen 栏目:开发技术

本篇内容介绍了“C++中的继承的概念和功能介绍”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

概述

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

类的概念

一个类中包含了若干数据成员和成员函数. 不同的类中的数据成员和成员函数各不相同. 但是有时两个类的内容基本相同. 例如:

C++中的继承的概念和功能介绍

继承的概念

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

  • 已存在的类: 基类 (base class) 或父类 (father class)

  • 新建立的类: 派生类 (derived class) 或子类 (son class)

C++中的继承的概念和功能介绍

一个新类从已有的类获得其已有特性, 称为类的继承.

  • 通过继承, 一个新建的子类从已有的父类那里获得父类的特性

  • 派生类继承了基类的所有数据成员和成员函数, 并可以对成员做必要的增加或调整

从已有的类 (父类) 产生一个新的子类, 称为类的派生.

  • 类的继承是用已有的类来建立专用新类的编程技术

  • 一个基类可以派生出多个派生类, 每一个派生类又可以作为基类再派生出新的派生类. 因此基类和派生类是相对而言的

  • 派生类是基类的具体化, 而基类则是派生类的抽象

单继承

单继承 (single inheritance) 指一个派生类只从一个基类派生.

  • 单继承关系形成的层次是一个树形结构

  • 箭头表示继承的方向, 从派生类指向基类

C++中的继承的概念和功能介绍

多重继承

多重继承 (multiple inheritance) 是指一个派生类有两个或多个基类. 派生类不仅可以从一个基类派生, 也可以从多个基类派生.

C++中的继承的概念和功能介绍

派生类

派生类的声明方式:

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

成员访问限定符 (默认 private):

  • public (公用的)

  • private (私有的)

  • protected (受保护的)

继承方式包括 (默认 private):

  • public (公用的)

  • private (私有的)

  • protected (受保护的)

Student 类:

#ifndef PROJECT5_STUDENT_H
#define PROJECT5_STUDENT_H

#include <string>
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 <iostream>
#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 <iostream>
#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;
}

mian:

#include <iostream>
#include "Student1.h"

int main() {
    Student1 student1(1, "Little White", 'f', 18, "火星");
    student1.show1();

    return 0;
}

输出结果:

number: 1
name: Little White
sex: f
age: 18
address: 火星

派生类中的成员包括从基类继承过来的成员和自己增加的成员两大部分. 每一部分布分别包括数据成员和成员函数.

派生类的构造函数和析构函数

构造函数和析构函数:

构造函数的主要作用是对数据成员初始化析构函数在释放对象前做一些相关的处理

因为派生类还继承了基类的数据成员. 设计派生类的构造函数时, 不仅要考虑派生类所增加的数据成员的初始化, 还应当考虑基类的数据成员初始化. 于是我们在执行派生类的构造函数时, 调用基类的构造函数.

派生类构造函数一般形式

派生类构造函数名 (总形式参数表列) : 基类构造函数名 (实际参数表列) {
    派生类中新增数据成员初始化语句
}
类内定义

在类内定义派生类构造函数:

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_H

class Base {
protected:
    Base();
    ~Base();
};

#endif //PROJECT5_BASE_H

Base.cpp:

#include <iostream>
#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 <iostream>
#include "Derived.h"
using namespace std;

Derived::Derived(char c) {
    cout << "子类构造函数, 值:" << c << endl;
}

Derived::~Derived() {
    cout << "子类析构函数" << endl;
}

main:

#include <iostream>
#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 <iostream>
#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 <iostream>
#include "Student1.h"
using namespace std;

int main() {
    Student1 student1(1, "Little White", 'f', 2, "班长", 'm', 18, "火星");
    student1.show1();

    return 0;
}

输出结果:

number: 1
name: Little White
sex: f
age: 18
address: 火星
==========班长信息==========
number: 2
name: 班长
sex: m

注意事项

  • 当不需要对派生类新增的成员函数进行任何初始化操作时, 派生类构造函数体可以为空

  • 基类没有构造函数或构造函数参数为空, 在派生类构造函数中可不写调用基类构造函数的语句, 盗用派生类构造函数时系统会自动调用基类的默认构造函数

  • 基类中定义了有参的构造函数, 派生类构造函数总必须写出基类的构造函数及其参数

  • 基类中既定义无参数的构造函数,又重载了有参数的构造函数, 派生类构造函数中可以调用带参的基类构造函数, 也可以不调用基类的构造函数

“C++中的继承的概念和功能介绍”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++
AI