Sams Teach Yourself C++ in 21 Days

(singke) #1
76:
77: cout << “Name: “;
78: cout << Edie.GetFirstName().GetString();
79: cout << “ “ << Edie.GetLastName().GetString();
80: cout << “.\nAddress: “;
81: cout << Edie.GetAddress().GetString();
82: cout << “.\nSalary: “ ;
83: cout << Edie.GetSalary();
84: return 0;
85: }

Name: Edythe Levine.
Address: 1461 Shore Parkway.
Salary: 50000
Listing 16.2 shows the Employeeclass, which contains three string objects (see
lines 26–28):itsFirstName,itsLastName, and itsAddress.
On line 71, an Employeeobject called Edieis created, and four values are passed in. On
line 72, the Employeeaccess function SetSalary()is called, with the constant value
50000. Note that in a real program, this would be either a dynamic value (set at runtime)
or a constant.
On line 73, a string called LastNameis created and initialized using a C++ string con-
stant. This string object is then used as an argument toSetLastName()on line 74.
On line 75, the Employeefunction SetFirstName()is called with yet another string con-
stant. However, if you are paying close attention, you will notice that Employeedoes not
have a function SetFirstName()that takes a character string as its argument;
SetFirstName()requires a constant string reference (see line 18).
The compiler resolves this because it knows how to make a string from a constant char-
acter string. It knows this because you told it how to do so on line 11 of Listing 16.1.
Looking at lines 78, 79, and 81, you see something that might appear unusual. You might
be wondering why GetString()has been tacked onto the different methods from the
Employeeclass:
78: cout << Edie.GetFirstName().GetString();
The Edieobject’s GetFirstName()method returns a String. Unfortunately, the String
object created in Listing 16.1 does not yet support the cout <<operator. To satisfy cout,
you need to return a C-Style string. GetString()is a method on your Stringobject that
returns a C-Style string. This problem will be fixed soon.

544 Day 16


LISTING16.2 continued

OUTPUT


ANALYSIS
Free download pdf