Team LiB
Previous Section Next Section

Chapter 15. Object-Oriented Programming

CONTENTS

Section 15.1 OOP: An Overview

558

Section 15.2 Defining Base and Derived Classes

560

Section 15.3 Conversions and Inheritance

577

Section 15.4 Constructors and Copy Control

580

Section 15.5 Class Scope under Inheritance

590

Section 15.6 Pure Virtual Functions

595

Section 15.7 Containers and Inheritance

597

Section 15.8 Handle Classes and Inheritance

598

Section 15.9 Text Queries Revisited

607

Chapter Summary

621

Defined Terms

621


Object-oriented programming is based on three fundamental concepts: data abstraction, inheritance, and dynamic binding. In C++ we use classes for data abstraction and class derivation to inherit one class from another: A derived class inherits the members of its base class(es). Dynamic binding lets the compiler determine at run time whether to use a function defined in the base or derived class.

面向对象编程基于三个基本概念:数据抽象、继承和动态绑定。在 C++ 中,用类进行数据抽象,用类派生从一个类继承另一个:派生类继承基类的成员。动态绑定使编译器能够在运行时决定是使用基类中定义的函数还是派生类中定义的函数。

Inheritance and dynamic binding streamline our programs in two ways: They make it easier to define new classes that are similar, but not identical, to other classes, and they make it easier for us to write programs that can ignore the details of how those similar types differ.

继承和动态绑定在两个方面简化了我们的程序:能够容易地定义与其他类相似但又不相同的新类,能够更容易地编写忽略这些相似类型之间区别的程序。

Many applications are characterized by concepts that are related but slightly different. For example, our bookstore might offer different pricing strategies for different books. Some books might be sold only at a given price. Others might be sold subject to some kind of discount strategy. We might give a discount to purchasers who buy a specified number of copies of the book. Or we might give a discount for only the first few copies purchased but charge full price for any bought beyond a given limit.

许多应用程序的特性可以用一些相关但略有不同的概念来描述。例如,书店可以为不同的书提供不同的定价策略,有些书可以只按给定价格出售,另一些书可以根据不同的折扣策略出售。可以给购买某书一定数量的顾客打折,或者,购买一定数量以内可以打折而超过给定限制就付全价。

Object-oriented programming (OOP) is a good match to this kind of application. Through inheritance we can define types that model the different kinds of books. Through dynamic binding we can write applications that use these types but that can ignore the type-dependent differences.

面向对象编程(Object-oriented programming,OOP)与这种应用非常匹配。通过继承可以定义一些类型,以模拟不同种类的书,通过动态绑定可以编写程序,使用这些类型而又忽略与具体类型相关的差异。

The ideas of inheritance and dynamic binding are conceptually simple but have profound implications for how we build our applications and for the features that programming languages must support. Before covering how C++ supports OOP, we'll look at the concepts that are fundamental to this style of programming.

继承和动态绑定的思想在概念上非常简单,但对于如何创建应用程序以及对于程序设计语言必须支持哪些特性,它们的含义深远。在讨论 C++ 如何支持面向对象编程之前,我们将介绍这种编程风格的一些基本概念。

Team LiB
Previous Section Next Section