|
|
 |
|
|
|
|
66: Employee Edie(Jane,Doe,1461 Shore Parkway, 20000);
67: Edie.SetSalary(50000);
68: String LastName(Levine);
69: Edie.SetLastName(LastName);
70: Edie.SetFirstName(Edythe);
71:
72: cout << Name: ;
73: cout << Edie.GetFirstName().GetString();
74: cout << << Edie.GetLastName().GetString();
75: cout << .\nAddress: ;
76: cout << Edie.GetAddress().GetString();
77: cout << .\nSalary: ;
78: cout << Edie.GetSalary();
79: return 0;
80: } |
|
|
|
|
|
|
|
|
Put the code from Listing 20.3 into a file called String.hpp. Then any time you need the String class you can include Listing 20.3 by using #include. For example, at the top of Listing 20.4 add the line #include String.hpp. This will add the String class to your program. |
|
|
|
|
|
|
|
|
|
Name; Edythe Levine
Address: 1461 Shore Parkway
Salary: 50000 |
|
|
|
|
|
|
|
|
Analysis: Listing 20.4 shows the Employee class, which contains three string objects: itsFirstName, itsLastName, and itsAddress. |
|
|
|
|
|
|
|
|
On line 66 an Employee object is created, and four values are passed in to initialize it. On line 67, the Employee access function SetSalary() is called, with the constant value 50000. Note that in a real program this would either be a dynamic value (set at runtime) or a constant. |
|
|
|
|
|
|
|
|
On line 68, a string is created and initialized using a C++ string constant. This string object is then used as an argument to SetLastName() on line 69. |
|
|
|
|
|
|
|
|
On line 70, the Employee function SetFirstName() is called with yet another string constant. However, if you are paying close attention you will notice that Employee does not have a function SetFirstName() that takes a character string as its argument; SetFirstName() requires a constant string reference. |
|
|
|
|
|
|
|
|
The compiler resolves this because it knows how to make a string from a constant character string. It knows this because you told it how to do so on line 9 of Listing 20.3. |
|
|
|
|
|