12 CHAPTER
CLASSES AND DATA
ABSTRACTION
I N T H I S C H A P T E R , YO U W I L L :
. Learn about classes
. Learn about private, protected, and public members of a class
. Explore how classes are implemented
. Examine constructors and destructors
. Learn about the abstract data type (ADT)
. Explore how classes are used to implement ADTs
. Learn about information hiding
. Explore how information hiding is implemented in C++
. Learn about the static members of a class
,650 | Chapter 12: Classes and Data Abstraction
In Chapter 11, you learned how to group data items that are of different types by using a
struct. The definition of a struct given in Chapter 11 is similar to the definition of a
C-struct. However, the members of a C++ struct can be data items as well as functions.
C++ provides another structured data type, called a class, which is specifically designed to
group data and functions. This chapter first introduces classes and explains how to use them
and then discusses the similarities and differences between a struct and a class.
Chapter 11 is not a prerequisite for this chapter. In fact, a struct and a class have similar
capabilities, as discussed in the section ‘‘A struct versus a class’’ in this chapter.
Classes
Chapter 1 introduced the problem-solving methodology called object-oriented design
(OOD). In OOD, the first step is to identify the components, called objects. An object
combines data and the operations on that data in a single unit. In C++, the mechanism
that allows you to combine data and the operations on that data in a single unit is called a
class. Now that you know how to store and manipulate data in computer memory and
how to construct your own functions, you are ready to learn how objects are constructed.
This and subsequent chapters develop and implement programs using OOD. This chapter
first explains how to define a class and use it in a program.
A class is a collection of a fixed number of components. The components of a class are
called the members of the class.
The general syntax for defining a class is:
class classIdentifier
{
classMembersList
};
in which classMembersList consists of variable declarations and/or functions. That is,
a member of a class can be either a variable (to store data) or a function.
• If a member of a class is a variable, you declare it just like any other
variable. Also, in the definition of the class, you cannot initialize a
variable when you declare it.
• If a member of a class is a function, you typically use the function
prototype to declare that member.
• If a member of a class is a function, it can (directly) access any member of the
class—member variables and member functions. That is, when you write
the definition of a member function, you can directly access any member
variable of the class without passing it as a parameter. The only obvious
condition is that you must declare an identifier before you can use it.
, Classes | 651
In C++, class is a reserved word, and it defines only a data type; no memory is
allocated. It announces the declaration of a class. Moreover, note the semicolon (;) after
the right brace. The semicolon is part of the syntax. A missing semicolon, therefore, will
result in a syntax error.
The members of a class are classified into three categories: private, public, and
protected. This chapter mainly discusses the first two types, private and public.
In C++, private, protected, and public are reserved words and are called member
access specifiers.
Following are some facts about public and private members of a class:
• By default, all members of a class are private.
• If a member of a class is private, you cannot access it outside of the
class. (Example 12-1 illustrates this concept.)
• A public member is accessible outside of the class. (Example 12-1
illustrates this concept.)
• To make a member of a class public, you use the member access
specifier public with a colon, :.
Suppose that we want to define a class to implement the time of day in a program.
Because a clock gives the time of day, let us call this class clockType. Furthermore, to
represent time in computer memory, we use three int variables: one to represent the
hours, one to represent the minutes, and one to represent the seconds.
Suppose these three variables are:
int hr;
int min;
int sec;
We also want to perform the following operations on the time:
1. Set the time.
2. Retrieve the time.
3. Print the time.
1
4. Increment the time by one second.
5. Increment the time by one minute. 2
6. Increment the time by one hour.
7. Compare the two times for equality.
To implement these seven operations, we will write seven functions—setTime, getTime,
printTime, incrementSeconds, incrementMinutes, incrementHours, and
equalTime.
From this discussion, it is clear that the class clockType has 10 members: three
member variables and seven member functions.
, 652 | Chapter 12: Classes and Data Abstraction
Some members of the class clockType will be private; others will be public.
Deciding which member to make public and which to make private depends on the
nature of the member. The general rule is that any member that needs to be accessed
outside of the class is declared public; any member that should not be accessed directly
by the user should be declared private. For example, the user should be able to set the
time and print the time. Therefore, the members that set the time and print the time
should be declared public.
Similarly, the members to increment the time and compare the time for equality should
be declared public. On the other hand, to prevent the direct manipulation of the
member variables hr, min, and sec, we will declare them private. Furthermore, note
that if the user has direct access to the member variables, member functions such as
setTime are not needed. The second part of this chapter (beginning with the section
‘‘Information Hiding’’) explains why some members need to be public and others
should be private.
The following statements define the class clockType:
class clockType
{
public:
void setTime(int, int, int);
void getTime(int&, int&, int&) const;
void printTime() const;
void incrementSeconds();
void incrementMinutes();
void incrementHours();
bool equalTime(const clockType&) const;
private:
int hr;
int min;
int sec;
};
In this definition:
• The class clockType has seven member functions: setTime,
getTime, printTime, incrementSeconds, incrementMinutes,
incrementHours, and equalTime. It has three member variables: hr,
min, and sec.
• The three member variables—hr, min, and sec—are private to the
class and cannot be accessed outside of the class. (Example 12-1 illustrates
this concept.)
• The seven member functions—setTime, getTime, printTime,
incrementSeconds, incrementMinutes, incrementHours, and
equalTime—can directly access the member variables (hr, min, and
sec). In other words, when we write the definitions of these functions,