9. Classes: A Deeper Look, Part 1

Objectives

In this chapter you'll learn:

  • How to use a preprocessor wrapper to prevent multiple definition errors caused by including more than one copy of a header file in a source-code file.

  • To understand class scope and accessing class members via the name of an object, a reference to an object or a pointer to an object.

  • To define constructors with default arguments.

  • How destructors are used to perform "termination housekeeping" on an object before it is destroyed.

  • When constructors and destructors are called and the order in which they are called.

  • The logic errors that may occur when a public member function of a class returns a reference to private data.

  • To assign the data members of one object to those of another object by default memberwise assignment.

My object all sublime I shall achieve in time.

W. S. Gilbert

Is it a world to hide virtues in?

William Shakespeare

Don't be "consistent," but be simply true.

Oliver Wendell Holmes, Jr.

This above all: to thine own self be true.

William Shakespeare

Outline

9.1 Introduction
9.2 Time Class Case Study
9.3 Class Scope and Accessing Class Members
9.4 Separating Interface from Implementation
9.5 Access Functions and Utility Functions
9.6 Time Class Case Study: Constructors with Default Arguments
9.7 Destructors
9.8 When Constructors and Destructors Are Called
9.9 Time Class Case Study: A Subtle Trap—Returning a Reference to a private Data Member
9.10 Default Memberwise Assignment
9.11 (Optional) Software Engineering Case Study: Starting to Program the Classes of the ATM System
9.12 Wrap-Up