Sams Teach Yourself C++ in 21 Days

(singke) #1
81: cout << Edie.GetFirstName().GetString();
82: cout << “ “ << Edie.GetLastName().GetString();
83: cout << “\nAddress: “;
84: cout << Edie.GetAddress().GetString();
85: cout << “\nSalary: “ ;
86: cout << Edie.GetSalary();
87: cout << endl;
88: return 0;
89: }

1: Creating Edie...
2: String(char*) constructor
3: String(char*) constructor
4: String(char*) constructor
5: Calling SetFirstName with char *...
6: String(char*) constructor
7: String destructor
8: Creating temporary string LastName...
9: String(char*) constructor
10: Name: Edythe Levine
11: Address: 1461 Shore Parkway
12: Salary: 20000
13: String destructor
14: String destructor
15: String destructor
16: String destructor
Listing 16.3 uses the same class declarations as Listings 16.1 and 16.2. However,
the coutstatements have been uncommented. The output from Listing 16.3 has
been numbered to make analysis easier.
On line 71 of Listing 16.3, the statement Creating Edie...is printed, as reflected on
line 1 of the output. On line 72, an Employeeobject,Edie, is created with four parame-
ters, the first three being strings. The output reflects the constructor for Stringbeing
called three times, as expected.
Line 74 prints an information statement, and then on line 75 is the statement
Edie.SetFirstName(“Edythe”). This statement causes a temporary string to be created
from the character string “Edythe”, as reflected on lines 5 and 6 of the output. Note that
the temporary Stringobject is destroyed immediately after it is used in the assignment
statement.
On line 77, a Stringobject is created in the body of the program. Here, the programmer
is doing explicitly what the compiler did implicitly on the previous statement. This time
you see the constructor on line 8 of the output, but no destructor. This object is not
destroyed until it goes out of scope at the end of the function.

548 Day 16


LISTING16.3 continued

OUTPUT


ANALYSIS
Free download pdf