Cost of Aggregation........................................................................................
When you have aggregated objects, there can be a cost in performance. Each time an
Employeestring is constructed or copied, you are also constructing each of its aggregated
string objects.
Uncommenting the coutstatements in Listing 16.1 reveals how often the constructors
are called. Listing 16.3 rewrites the driver program to add print statements indicating
where in the program objects are being created. Uncomment the lines in Listing 16.1,
and then compile Listing 16.3.
546 Day 16
To compile this listing, uncomment lines 40, 53, 65, 77, 86, and 102 in
Listing 16.1.
NOTE
LISTING16.3 Aggregated Class Constructors
0: //Listing 16.3 Aggregated Class Constructors
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; }
22: void SetAddress(const String & address)
23: { itsAddress = address; }
24: void SetSalary(long salary) { itsSalary = salary; }
25: private:
26: String itsFirstName;
27: String itsLastName;
28: String itsAddress;
29: long itsSalary;