Skip to Content
介绍 C++ (Chinese Edition)
book

介绍 C++ (Chinese Edition)

by Frances Buontempo
March 2026
Intermediate
348 pages
3h 59m
Chinese
O'Reilly Media, Inc.
Content preview from 介绍 C++ (Chinese Edition)

第 13 章 类 :虚函数与继承

你在第 10 章中接触了类,并在第 11 章中深入探讨了特殊成员函数。 你还在第 12 章中学习了如何使用智能指针。 在本章中,你将利用这些构建模块来构建一个更完善的交易游戏。

你可以构建类层次结构:这是一种将类从抽象的通用概念关联到更具体类型的手段()。 随后,你可以在相关类中实现行为的差异化。 你的Stock 类使用一种方法获取价格,但你已经见过其他方法。 本章将向你展示如何提供相关类,这些类使用不同的策略来为资产定价。 利用通过类层次结构()关联的不同类来实现行为差异化——即类型家族——这被称为面向对象编程(OOP)。

如果你在具有类层次结构的现有代码库上工作,可以轻松添加一个新的相关类来改变行为,例如为应用程序添加新功能。 如果没有这些相关类,你可能需要编写包含大量if/else 语句的庞大函数来实现行为变化。 添加新功能可能意味着要增加更多的ifelse 语句,最终导致函数变得臃肿且难以理解。 面向对象编程为你提供了一种(相对)简单的方式来添加新功能。

我将向您演示如何从通用Asset类派生出具体的Stock 类,以便您在新的交易游戏中使用它。 随后,您将在“添加另一个派生类型”中创建另一个类,从而能够改变游戏玩法,而无需修改游戏代码本身

你还将有机会复习已掌握的 C++ 特性,包括static_assert和异常处理。

基类与派生类

类层次结构将相关的类按层级排列。 最佳的结构是将抽象类作为其他类的基类()。 当一个类声明了某些函数但未实现它们时,该类即为抽象类。 抽象类()提供了一个接口:即对相关类可执行操作的描述。

您可以持有对抽象类的引用或指针,但无法创建其实例。 其他类可以从基类派生,因此您通常会引用或指向派生类。 派生类对基类中未实现的函数有自己的实现,从而允许行为发生变化。 这非常有用,因为你可以针对接口(即基类)进行编程,而无需担心细节,并且可以通过添加新的派生类来改变行为,而无需修改使用该接口的代码。

定义抽象基类

让我们先从一个基类开始,稍后看看如何从它派生出其他类。 使用关键字virtual 可以指示成员函数在派生类中可以以不同的方式重写。 你也可以在基类中添加= 0,而不是提供实现。 这种成员函数被称为纯虚函数:即没有实现的函数。 此时基类就是抽象的:你无法创建它的实例。

创建一个名为asset.h 的新文件。 原始的Stock类包含get_namenext_price函数。 对于更通用的资产,你需要这两个函数。 添加一个返回当前价格的函数可能会很有用。 在stock_prices命名空间内,添加一个名为Asset 的新类,其中包含三个抽象虚函数。 你还需要提供一个虚析构函数,原因我将在“深入探讨虚函数与继承”中解释

#pragma once

#include <string>

namespace stock_prices
{
    class Asset
    {
    public:
        virtual ~Asset() = default; 1
        virtual std::string get_name() const = 0; 
        virtual double get_price() const = 0; 
        virtual double next_price() = 0;
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Navigating the midcareer plateau

Navigating the midcareer plateau

Uma Chingunde
Run Llama-2 Models

Run Llama-2 Models

Federico Castanedo

Publisher Resources

ISBN: 0642572352684