Account
ObjectClasses cannot execute by themselves. A Person
object can drive a Car
object by telling it what to do (go faster, go slower, turn left, turn right, etc.)—without knowing how the car’s internal mechanisms work. Similarly, the main
function can “drive” an Account
object by calling its member functions—without knowing how the class is implemented. In this sense, main
is referred to as a driver program. We show the main
program and its output first, so you can see an Account
object in action. To help you prepare for the larger programs you’ll encounter later in this book and in industry, we define main
in its own file (file AccountTest.cpp
, Fig. 3.1). We define class Account
in its own file as well (file Account.h
, Fig. 3.2).
Typically, you cannot call a member function of a class until you create an object of that class.3 Line 10
Account myAccount; // create Account object myAccount
creates an object of class Account
called myAccount
. The variable’s type is Account
—the class we define in Fig. 3.2.
When we declare variables of type int
, as we did in Chapter 2, the compiler knows what int
is—it’s a fundamental type that’s “built into” C++. In line 10, however, the compiler does not know in advance what type Account
is—it’s a user-defined type.
When packaged properly, new classes can be reused by other programmers. It’s customary to place a reusable class definition in a file known as a header with a .h
filename extension.4 You include (via #include
) that header wherever you need to use the class. For example, you can reuse the C++ Standard Library’s classes in any program by including the appropriate headers.
Class Account
is defined in the header Account.h
(Fig. 3.2). We tell the compiler what an Account
is by including its header,
as in line 5 (Fig. 3.1):
#include "Account.h"
If we omit this, the compiler issues error messages wherever we use class Account
and any of its capabilities. In an #include
directive, a header that you define in your program is placed in double quotes (""
), rather than the angle brackets (<>
) used for C++ Standard Library headers like <iostream>
. The double quotes in this example tell the compiler that header is in the same folder as Fig. 3.1, rather than with the C++ Standard Library headers.
Files ending with the .cpp
filename extension are source-code files. These define a program’s main
function, other functions and more, as you’ll see in later chapters. You include headers into source-code files (as in Fig. 3.1), though you also may include them in other headers.
Account’s getName
Member FunctionThe Account
class’s getName
member function returns the account name stored in a particular Account
object. Line 13
cout << "Initial name is: " << myAccount.getName();
displays myAccount
’s initial name by calling the object’s getName
member function with the expression myAccount.getName()
. To call this member function for a specific object, you specify the object’s name (myAccount
), followed by the dot operator (.), then the member function name (getName
) and a set of parentheses. The empty parentheses indicate that getName
does not require any additional information to perform its task. Soon, you’ll see that the setName
function requires additional information to perform its task.
From main
’s view, when the getName
member function is called:
The program transfers execution from the call (line 13 in main
) to member function getName
. Because getName
was called via the myAccount
object, getName
“knows” which object’s data to manipulate.
Next, member function getName
performs its task—that is, it returns (i.e., gives back) myAccount
’s name to line 13 where the function was called. The main function does not know the details of how getName
performs its task.
The cout
object displays the name returned by member function getName
, then the program continues executing at line 16 in main
.
In this case, line 13 does not display a name, because we have not yet stored a name in the myAccount
object.
string
with getline
Line 17
string theName;
creates a string variable called theName
that’s used to store the account name entered by the user. string
variables can hold character string values such as "Jane
Green"
. A string
is actually an object of the C++ Standard Library class string
, which is defined in the header <string>.5 The class name string
, like the name cout
, belongs to namespace std
. To enable line 17 to compile, line 4 includes the <string>
header. The using
directive in line 7 allows us to write string
in line 17 rather than std::string
.
getline
Function Receiving a Line of Text from the UserSometimes functions are not members of a class. Such functions are called global functions. Line 18
getline(cin, theName); // read a line of text
reads the name from the user and places it in the variable theName
, using the C++ Standard Library global function getline to perform the input. Like class string
, function getline
requires the <string>
header and belongs to namespace std
.
Consider why we cannot simply write
cin >> theName;
to obtain the account name. In our sample program execution, we entered the name “Jane Green
,” which contains multiple words separated by a space. (Recall that we highlight user inputs in bold in our sample program executions.) When reading a string
, cin
stops at the first white-space character (such as a space, tab or newline). Thus, the preceding statement would read only "Jane"
. The information after "Jane"
is not lost—it can be read by subsequent input statements later in the program.
In this example, we’d like the user to type the complete name (including the space) and press Enter to submit it to the program. Then, we’d like to store the entire name in the string
variable theName
. When you press Enter (or Return) after typing data, the system inserts a newline in the input stream. Function getline
reads from the standard input stream object cin
the characters the user enters, up to, but not including, the newline, which is discarded; getline
places the characters in the string
variable theName
.
Account’s setName
Member FunctionThe Account
class’s setName
member function stores an account name in a particular Account
object. Line 19
myAccount.setName(theName); // put theName in myAccount
calls myAccounts
’s setName
member function. A member-function call can supply arguments that help the function perform its task. You place the arguments in the function call’s parentheses. Here, theName
’s value (input by line 18) is the argument that’s passed to setName
, which stores theName
’s value in the object myAccount
.
From main
’s view, when setName
is called:
The program transfers execution from line 19 in main
to setName
member function’s definition. The call passes to the function the argument value in the call’s parentheses—that is, theName
object’s value. Because setName
was called via the myAccount
object, setName
“knows” the exact object to manipulate.
Next, member function setName
stores the argument’s value in the myAccount
object.
When setName
completes execution, program execution returns to where setName
was called (line 19), then continues at line 22.
To demonstrate that myAccount
now contains the name the user entered, lines 22–23
cout << "Name in object myAccount is: "
<< myAccount.getName() << endl;
call member function getName
again. As you can see in the last line of the program’s output, the name entered by the user in line 18 is displayed. When the preceding statement completes execution, the end of main
is reached, so the program terminates.