Sams Teach Yourself C++ in 21 Days

(singke) #1
Listing 16.1 provides a Stringclass much like the one used in Listing 13.12 of
Day 13. The significant difference here is that the constructors and a few other
functions in Listing 13.12 have printstatements to show their use, which are currently
commented out in Listing 16.1. These functions will be used in later examples.
On line 25, the static member variable ConstructorCountis declared, and on line 156 it
is initialized. This variable is incremented in each string constructor. All this is currently
commented out; it will be used in a later listing.
For convenience, the implementation is included with the declaration of the class. In a
real-world program, you would save the class declaration in String.hppand the imple-
mentation in String.cpp. You would then add String.cppinto your program (using add
files or a make file) and have String.cpp #include String.hpp.
Of course, in a real program, you’d use the C++ Standard Library String class, and not
this string class in the first place.
Listing 16.2 describes an Employeeclass that contains three string objects. These objects
are used to hold an employee’s first and last names as well as their address.

LISTING16.2 The EmployeeClass and Driver Program
0: // Listing 16.2 The Employee Class and Driver Program
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; }

542 Day 16


ANALYSIS
Free download pdf