9.12. Wrap-Up
This chapter deepened our coverage
of classes, using a rich Time class case
study to introduce several new features of classes. You saw that member
functions are usually shorter than global functions because member functions can
directly access an object's data members, so the member functions can receive
fewer arguments than functions in procedural programming languages. You learned
how to use the arrow operator to access an object's members via a pointer of the
object's class type.
You learned that member
functions have class scope—i.e., the member function's name is known only to
other members of the class unless referred to via an object of the class, a
reference to an object of the class, a pointer to an object of the class or the
binary scope resolution operator. We also discussed access functions (commonly
used to retrieve the values of data members or to test the truth or falsity of
conditions) and utility functions (private
member functions that support the operation of the class's public
member functions).
You saw that a constructor can
specify default arguments that enable it to be called in a variety of ways. You
also saw that any constructor that can be called with no arguments is a default
constructor and that there can be at most one default constructor per class. We
discussed destructors and their purpose of performing termination housekeeping
on an object of a class before that object is destroyed. We also demonstrated
the order in which an object's constructors and destructors are called.
We demonstrated the problems
that can occur when a member function returns a reference to a
private data member, which breaks the
encapsulation of the class. We also showed that objects of the same type can be
assigned to one another using default memberwise assignment. Finally, we
discussed the benefits of using class libraries to enhance the speed with which
code can be created and to increase the quality of software.
Chapter
10 presents additional class features. We'll demonstrate how
const can be used to indicate that a member
function does not modify an object of a class. You'll see how to build classes
with composition—the capability that allows a class to contain objects of other
classes as members. We'll show how a class can allow so-called "friend"
functions to access the class's non-public
members. We'll also show how a class's non-static
member functions can use a special pointer named this to access an object's members. Next, we discuss how to
use C++'s new and delete operators, which
enable you to obtain and release memory as necessary during a program's
execution.