Team LiB
Previous Section Next Section

Chapter 15. Object-Oriented Programming


Section 15.1 OOP: An Overview


Section 15.2 Defining Base and Derived Classes


Section 15.3 Conversions and Inheritance


Section 15.4 Constructors and Copy Control


Section 15.5 Class Scope under Inheritance


Section 15.6 Pure Virtual Functions


Section 15.7 Containers and Inheritance


Section 15.8 Handle Classes and Inheritance


Section 15.9 Text Queries Revisited


Chapter Summary


Defined Terms


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