10.1. Introduction
In this chapter, we continue our
study of classes and data abstraction with several more advanced topics. We use
const objects and const member
functions to prevent modifications of objects and enforce the principle of least
privilege. We discuss composition—a form of reuse in which a class can have
objects of other classes as members. Next, we introduce friendship, which
enables a class designer to specify nonmember functions that can access class's
non-public members—a technique that is often used
in operator overloading (Chapter
11) for performance reasons. We discuss a special pointer (called
this), which is an implicit argument to each of a class's
non-static member functions. It allows those
member functions to access the correct object's data members and other
non-static member functions. We then
discuss dynamic memory management and show how to create and destroy objects
dynamically with the new and delete
operators. Next, we motivate the need for static
class members and show how to use static data
members and member functions in your own classes. Finally, we show how to create
a proxy class to hide the implementation details of a class (including its
private data members) from clients of the
class.
Recall that Chapter
3 introduced C++ Standard Library class string to represent strings as full-fledged class objects. In
this chapter, however, we use the pointer-based strings we introduced in Chapter
8 to help you master pointers and prepare for the
professional world in which you'll see a great deal of C legacy code implemented
over the last several decades. Thus, you'll become familiar with the two most
prevalent methods of creating and manipulating strings in C++.