Chapter 2. Object-Oriented Design

Object-Oriented Design

Contents

2.1 Goals, Principles, and Patterns

66

2.1.1 Object-Oriented Design Goals

66

2.1.2 Object-Oriented Design Principles

67

2.1.3 Design Patterns

70

2.2 Inheritance and Polymorphism

71

2.2.1 Inheritance in C++

71

2.2.2 Polymorphism

78

2.2.3 Examples of Inheritance in C++

79

2.2.4 Multiple Inheritance and Class Casting

84

2.2.5 Interfaces and Abstract Classes

87

2.3 Templates

90

2.3.1 Function Templates

90

2.3.2 Class Templates

91

2.4 Exceptions

93

2.4.1 Exception Objects

93

2.4.2 Throwing and Catching Exceptions

94

2.4.3 Exception Specification

96

2.5 Exercises

98

Goals, Principles, and Patterns

As the name implies, the main "actors" in the object-oriented design paradigm are called objects. An object comes from a class, which is a specification of the data members that the object contains, as well as the member functions (also called methods or operations) that the object can execute. Each class presents to the outside world a concise and consistent view of the objects that are instances of this class, without going into too much unnecessary detail or giving others access to the inner workings of the objects. This view of computing is intended to fulfill several goals and incorporate several design principles, which we discuss in this chapter.

Object-Oriented Design Goals

Software implementations should achieve robustness, adaptability, and reusability. (See Figure 2.1.)

Figure 2.1. Goals ...

Get Data Structures and Algorithms in C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.