Advanced Inheritance 549
16
On lines 81–87, the strings in the Employeeobject are destroyed as the Employeeobject
falls out of scope, and the string LastName, created on line 77, is destroyed as well when
it falls out of scope.
Copying by Value ..........................................................................................
Listing 16.3 illustrates how the creation of one Employeeobject caused five string con-
structor calls. Listing 16.4 again rewrites the driver program. This time, the print state-
ments are not used, but the string static member variable ConstructorCountis
uncommented and used.
Examination of Listing 16.1 shows that ConstructorCountis incremented each time a
string constructor is called. The driver program in 16.4 calls the print functions, passing in
the Employeeobject, first by reference and then by value. ConstructorCountkeeps track
of how many string objects are created when the employee is passed as a parameter.
To compile this listing, leave in the lines that you uncommented in Listing
16.1 to run Listing 16.3, and in addition, uncomment lines 25, 41, 54, 66, 78,
and 155 from Listing 16.1.
NOTE
LISTING16.4 Passing by Value
0: // Listing 16.4 Passing by Value
1: #include “MyString.hpp”
2:
3: class Employee
4: {
5: public:
6: Employee();
7: Employee(char *, char *, char *, long);
8: ~Employee();
9: Employee(const Employee&);
10: Employee & operator= (const Employee &);
11:
12: const String & GetFirstName() const
13: { return itsFirstName; }
14: const String & GetLastName() const { return itsLastName; }
15: const String & GetAddress() const { return itsAddress; }
16: long GetSalary() const { return itsSalary; }
17:
18: void SetFirstName(const String & fName)
19: { itsFirstName = fName; }
20: void SetLastName(const String & lName)
21: { itsLastName = lName; }