3.2. Classes, Objects, Member Functions
and Data Members
Let's begin with a simple analogy to
help you reinforce your understanding from Section
1.10 of classes and their contents. Suppose you
want to drive a car and make it go faster by pressing down on its accelerator
pedal. What must happen before you can do this? Well, before you can drive a
car, someone has to design it and build it. A car typically begins as
engineering drawings, similar to the blueprints used to design a house. These
drawings include the design for an accelerator pedal that the driver will use to
make the car go faster. In a sense, the pedal "hides"
the complex mechanisms that actually make the car go faster, just as the brake
pedal "hides" the mechanisms that slow the car, the steering wheel "hides" the
mechanisms that turn the car and so on. This enables people with little or no
knowledge of how cars are engineered to drive a car easily, simply by using the
accelerator pedal, the brake pedal, the steering wheel, the transmission
shifting mechanism and other such simple and user-friendly "interfaces" to the
car's complex internal mechanisms.
Unfortunately, you cannot drive the
engineering drawings of a car—before you can drive a car, it must be built from
the engineering drawings that describe it. A completed car will have an actual
accelerator pedal to make the car go faster. But even that's not enough—the car
will not accelerate on its own, so the driver must press the accelerator pedal
to tell the car to go faster.
Classes and Member Functions
Now let's use our car example to
introduce the key object-oriented programming concepts of this section.
Performing a task in a program requires a function (such as main, as
described in Chapter
2). The function describes the mechanisms that
actually perform its tasks. The function hides from its user the complex tasks
that it performs, just as the accelerator pedal of a car hides from the driver
the complex mechanisms of making the car go faster. In C++, we begin by creating
a program unit called a class to house a function, just as a car's engineering
drawings house the design of an accelerator pedal. Recall from Section
1.10 that a function belonging to a class is called
a member function. In a class, you provide one or more member functions that are
designed to perform the class's tasks. For example, a class that represents a
bank account might contain one member function to deposit money into the
account, another to withdraw money from the account and a third to inquire what
the current account balance is.
Objects
Just as you cannot drive an engineering
drawing of a car, you cannot "drive" a class. Just as someone has to build a car
from its engineering drawings before you can actually drive the car, you must
create an object of a class before you can get a program to perform the tasks
the class describes. That is one reason C++ is known as an object-oriented
programming language. Note also that just as many
cars can be built from the same engineering drawing, many objects can be built from the same class.
Requesting an Object's Services via
Member-Function Calls
When you drive a car, pressing its gas
pedal sends a message to the car to perform a task—that is, make the car go
faster. Similarly, you send messages to an object—each message is known as a member-function call and tells a member function of the object to perform
its task. This is often called requesting a service from an object.
Attributes and Data Members
Thus far, we have used the car analogy
to introduce classes, objects and member functions. In addition to the
capabilities a car provides, it also has many attributes, such as its color, the
number of doors, the amount of gas in its tank, its current speed and its total
miles driven (i.e., its odometer reading). Like the car's capabilities, these
attributes are represented as part of a car's design in its engineering
diagrams. As you drive a car, these attributes are always associated with the
car. Every car maintains its own attributes. For example, each car knows how much gas is in its own gas tank, but not
how much is in the tanks of other cars. Similarly, an object has attributes that
are carried with the object as it is used in a program. These attributes are
specified as part of the object's class. For example, a bank account object has
a balance attribute that represents the amount of money in the account. Each
bank account object knows the balance in the account it represents, but not the
balances of the other accounts in the bank. Attributes are specified by the
class's data members.