• 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 amember of a class is a function, it can (directly) access anymember 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.
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 11-1 illustrates this concept.)
• A public member is accessible outside of the class. (Example 11-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.
Functions and Classes
The following rules describe the relationship between functions and classes:
• Class objects can be passed as parameters to functions and returned as
function values.
• As parameters to functions, class objects can be passed either by value or
by reference.
• If a class object is passed by value, the contents of the member variables of
the actual parameter are copied into the corresponding member variables
of the formal parameter.
Reference Parameters and Class Objects (Variables)
Recall that when a variable is passed by value, the formal parameter copies the value of
the actual parameter. That is, memory space to copy the value of the actual parameter is
allocated for the formal parameter. As a parameter, a class object can be passed by value.
Suppose that a class has several member variables requiring a large amount of memory to store
data, and you need to pass a variable by value. The corresponding formal parameter then
receives a copy of the data of the variable. That is, the compiler must allocate memory for the
formal parameter, so as to copy the value of the member variables of the actual parameter.
This operation might require, in addition to a large amount of storage space, a considerable
amount of computer time to copy the value of the actual parameter into the formal parameter.
On the other hand, if a variable is passed by reference, the formal parameter receives only the
address of the actual parameter. Therefore, an efficient way to pass a variable as a parameter is
by reference. If a variable is passed by reference, then when the formal parameter changes, the
actual parameter also changes. Sometimes, however, you do not want the function to be able
to change the values of the member variables. In C++, you can pass a variable by reference
and still prevent the function from changing its value by using the keyword const in the
formal parameter declaration. As an example, consider the following function definition:
Implementation of Member Functions
When we defined the class clockType, we included only the function prototype
for the member functions. For these functions to work properly, we must write the
related algorithms. One way to implement these functions is to provide the function
definition rather than the function prototype in the class itself. Unfortunately, the
class definition would then be very long and difficult to comprehend. Another reason
for providing function prototypes instead of function definitions relates to information
hiding; that is, we want to hide the details of the operations on the data. We
will discuss this issue later in this chapter, in the section ‘‘Information Hiding.’’
Next, let us write the definitions of the member functions of the class clockType.
That is, we will write the definitions of the functions setTime, getTime, printTime,
incrementSeconds, equalTime, and so on. Because the identifiers setTime,
printTime, and so forth are local to the class, we cannot reference them (directly) outside
of the class. In order to reference these identifiers, we use the scope resolution operator, ::
(double colon). In the function definition’s heading, the name of the function is the name of
the class, followed by the scope resolution operator, followed by the function name. For
example, the definition of the function setTime is as follows: