Composition and Class Interfaces/Abstract Class

Having trouble trying to get my calculatePay() to work. Won't take the annualSalary and divide it by 52 like it is suppose to. Here is what I got everything else writes to the screen just fine.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
  //Program Header
//Program Name: Class Development
//Programmer: Jeffrey Bell
//CIS247C, Week 4 Lab
//Program Description: The objective of the lab is to modify the Employee class to demonstrate
//composition and a class interface.
//	


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void DisplayDivider(string);
string GetInput(string);

class Benefit
{
private:
	string healthInsurance;
	double lifeInsurance;
	int vacation;
public:
	Benefit();
	Benefit(string, double, int);
	void displayBenefits();
	string getHealthInsurance();
	void setHealthInsurance(string);
	double getLifeInsurance();
	void setLifeInsurance(double);
	int getVacation();
	void setVacation(int);
};


Benefit::Benefit()
{
	healthInsurance = "Not Provided";
	lifeInsurance = 0.0;
	vacation = 14;
}

Benefit::Benefit(string health, double life, int vaca)
{
	healthInsurance = health;
	lifeInsurance = life;
	vacation = vaca;
}

void Benefit::displayBenefits()
{
	cout << "Employee Benefit Information" << endl;
	cout << "-----------------------------------------" << endl;
	cout << "Health Insurance: " << healthInsurance << endl;
	cout << "Life Insurance: " << lifeInsurance << endl;
	cout << "Vacation: " << vacation << " days" << endl;
}

string Benefit::getHealthInsurance()
{
	return healthInsurance;
}


void Benefit::setHealthInsurance(string hins)
{
	healthInsurance = hins;
}

double Benefit::getLifeInsurance()
{
	return lifeInsurance;
}

void Benefit::setLifeInsurance(double lins)
{
	lifeInsurance = lins;
}

int Benefit::getVacation()
{
	return vacation;
}

void Benefit::setVacation(int vaca)
{
	vacation = vaca;
}

class iEmployee
{
public:
	virtual double calculatePay() = 0;

};


class Employee : public iEmployee
{
private:
	string firstName;
	string lastName;
	char gender;
	int dependents;
	double annualSalary;
	static int numEmployees;

public:
	Employee();
	Employee(string, string, char, int, double, Benefit);
	double calculatePay();
	void displayEmployee();
	string getFirstName();
	void setFirstName(string);
	string getLastName();
	void setLastName(string);
	char getGender();
	void setGender(char);
	int getDependents();
	void setDependents(int);
	double getAnnualSalary();
	void setAnnualSalary(double&);
	static int getNumEmployees();
	void setDependents(string);
	void setAnnualSalary(string);
	Benefit benefit;
};
int Employee::numEmployees = 0;

Employee::Employee() : benefit()
	{
	firstName = "not given";
	lastName = "not given";
	gender = 'U';
	dependents = 0;
	annualSalary = 20000;
	benefit;
	numEmployees++;
}

Employee::Employee(string first, string last, char gen, int dep, double salary, Benefit ben) 
{
	firstName = first;
	lastName = last;
	gender = gen;
	dependents = dep;
	annualSalary = salary;
	benefit = ben;
	numEmployees++;

}


double Employee::calculatePay()
{
	return annualSalary / 52;
}

void Employee::displayEmployee()
{
	cout << "Employee Information" << endl;
	cout << "-----------------------------------------" << endl;
	cout << "Name: " << firstName << " " << lastName << endl;
	cout << "Gender: " << gender << endl;
	cout << "Dependents: " << dependents << endl;
	cout << "Annual Salary: " << annualSalary << endl;
	Employee::calculatePay();
	cout << "Weekly Salary: " << annualSalary << endl;
	benefit.displayBenefits();
	
}

string Employee::getFirstName()
{

	return firstName;
}

void Employee::setFirstName(string first)
{
	firstName = first;
}

string Employee::getLastName()
{

	return lastName;
}

void Employee::setLastName(string last)
{
	lastName = last;
}

char Employee::getGender()
{

	return gender;
}

void Employee::setGender(char gen)
{
	gender = gen;
}

int Employee::getDependents()
{

	return dependents;
}

void Employee::setDependents(int dep)
{
	dependents = dep;
}

void Employee::setDependents(string dep)
{
	dependents = atoi(dep.c_str());
}

double Employee::getAnnualSalary()
{

	return annualSalary;
}

void Employee::setAnnualSalary(double &salary)
{
	annualSalary = salary;
}

void Employee::setAnnualSalary(string salary)
{
	annualSalary = atof(salary.c_str());
}

int Employee::getNumEmployees()
{
	return numEmployees;
}


int main()
{
	Employee Employee1;
	
	string firstName;
	string lastName;
	string input;
	char gender;
	string dependents;
	string annualSalary;
	string healthInsurance;
	double lifeInsurance;
	int vacation;

	DisplayDivider(" Employee 1 ");
	firstName = GetInput("Frist Name: ");
	lastName = GetInput("Last Name: ");
	input = GetInput("Gender: ");
	gender = input.at(0);
	dependents = GetInput("Dependents: ");
	annualSalary = GetInput("Annual Salary: ");


	Employee1.getFirstName();
	Employee1.getLastName();
	Employee1.getGender();
	Employee1.getDependents();
	Employee1.getAnnualSalary();

	Employee1.setFirstName(firstName);
	Employee1.setLastName(lastName);
	Employee1.setGender(gender);
	Employee1.setDependents(dependents);
	Employee1.setAnnualSalary(annualSalary);

	healthInsurance = GetInput("Health Insurance: ");
	input = GetInput("Life Insurance: ");
	lifeInsurance = atof(input.c_str());
	input = GetInput("Vacation: ");
	vacation = atoi(input.c_str());
	
	Employee1.benefit.setHealthInsurance(healthInsurance);
	Employee1.benefit.setLifeInsurance(lifeInsurance);
	Employee1.benefit.setVacation(vacation);

	

	DisplayDivider(" Employee Information ");

	cout << setprecision(2) << showpoint << fixed << "\n";

	Employee1.displayEmployee();

	
	DisplayDivider("--- Number of Employee Objects Created ---");
	cout << "Number of employees: " << Employee::getNumEmployees() << endl;
	cout << endl;

	Benefit benefit1("Blue Cross",15.0, 34);

	DisplayDivider(" Employye 2 ");
	Employee Employee2("Mary", "Noia", 'F', 5, 24000.0, benefit1);
	Employee2.displayEmployee();

	DisplayDivider("--- Number of Employee Objects Created ---");
	cout << "Number of employees: " << Employee::getNumEmployees() << endl;
	cout << endl;

	return 0;
}

void DisplayDivider(string outputTitle)
{
	cout << "********** " << outputTitle << " ***********\n" << endl;

}

string GetInput(string inputType)
{
	string strInput;
	cout << "Please Enter your " << inputType << endl;
	getline(cin, strInput);

	return strInput;
}
Is it because you output annualSalary for the weekly salary?

Also, you misspelled First as in "First Name".
Last edited on
My calculatePay Function returns annualSalary which in turn would overwrite the existing one right. I had this all working on my last program but we ended up adding the Benefit class and the iEmployee. Since I added the iEmployee it only returns the annualSalaray back without out doing the conversion. Should i add another variable in there?
I tried changing the calculatePay function by adding in a new variable, i took weeklysalary = annualSalary / 52 than returned weekly salary and that didn't work either.
Your calculatePay() function does a calculation but you're not assigning it to anything: It just returns the calculation, which is fine. Wouldn't this work?:

1
2
3
4

	cout << "Weekly Salary: " << Employee::calculatePay() << endl;


Well that worked perfectly, thanks for the help I appreciate it. Funny all I had to do was replace the way I had it with the way you stated and it worked just fine. Thanks again
You're welcome. Your code is very good and very well thought-out.

If you're satisfied with my answer, please mark this as solved.
Hi,

Here is some advice to help you out in the future, am putting some ideas & concepts out there, some of it may be a shade advanced right now, but there you go :+)

Just wondering what is happening here?

279
280
281
282
283
284
285
286
287
288
289
	Employee1.getFirstName();
	Employee1.getLastName();
	Employee1.getGender();
	Employee1.getDependents();
	Employee1.getAnnualSalary();

	Employee1.setFirstName(firstName);
	Employee1.setLastName(lastName);
	Employee1.setGender(gender);
	Employee1.setDependents(dependents);
	Employee1.setAnnualSalary(annualSalary);


The get functions are accessing un-initialised member data as far as I can see. Better to set them first?

A better way of doing things is to use an initialiser list in a constructor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
                         // spread these out parameters 1 per line - easier to read
                          // and can be commented - do this for any function that has multiple parameters
Employee::Employee (
                                // variables here for any base class if necessay
                                const std::string& first ,   // Pass by reference for string
                                const std::string& last ,    // 
                                const char gen ,              // Pass by value for basic types
                                const int dep ,                 // could be unsigned short int?
                                const double salary ,
                                const Benefit& ben ,        // Pass by reference for user defined types
                                // numEmployees         // not included because its static - do you even need it?
                                                                  // can get size of container from it 's member function
) 
              :       // colon introduces initialiser list
                      // member variables are initialised before 
                      // construction, not assigned to again after construction

                      // make sure to do each member variable, don't miss any
        // call any base class constructors if needed  (in order) here to avoid object slicing
	firstName (first) ,     // can put comments here too
	lastName (last),
	gender (gen) ,
	dependents (dep) , 
	annualSalary (salary) ,
	benefit (ben) ,
        // numEmployees         // not included because its static
{
	// numEmployees++; // probably don't need it

}



Doing things this way means you probably don't need all those set functions.

Set functions are only required when the object is changed / updated after it has been created. Even then it is often possible to think about what has to happen to an object, then write a function that updates several related or all of the member variables at once.

For updating, one might have a member UpdateAddress function as an general example. Remember member functions have direct access to member variables - so one would do that rather than call lots of set functions.

Another example for output one could write a member PrintDetails function, rather than calling all the get functions. Or overload the ostream << operator.

So as it stands at the moment, you don't seem to need any get or set functions that I can see.

Instead of making an Employee1 object, consider using a std::vector (if allowed to) or at least an array of them. Use the size function which belongs to std::vector to reveal how many employees you have. Or count them as you put them into the array.


With get functions (if you are going to have them), they should be marked const, because they don't change the state of the object - that is the values of all the member variables remain the same. Parameters to functions should also be marked const where possible, one usually doesn't want these to be altered in the body of the function (example below), unless passing by reference in order to modify a variable in the calling scope. Also, they should return const values or a const reference, one generally doesn't want anyone to change the returned value afterwards:

1
2
3
4
5
const string& Employee::getFirstName() const
{

	return firstName;
}


1
2
3
4
5
const double Employee::getAnnualSalary() const
{

	return annualSalary;
}


This is all called const correctness - on should use const wherever possible. Note that a const function is separate to a non const one, even though their signatures are otherwise the same:

1
2
3
4
5
6
7
8
9
const double MyClass::MyDoubleFunc(const double MyDouble1 , const double MyDouble2) const {
     //code that doesn't change the state of the class
     return MyDouble  ;
}

const double MyClass::MyDoubleFunc(const double MyDouble1 , const double MyDouble2)  {
     // code that might change the state of the class
     return MyDouble;
}


As well as that, returning a reference to a local variable doesn't work - it gets destroyed as soon as it goes out of scope:

1
2
3
4
5
 std::string& MyFunction () {
      // local variable
      std::string MyString("Me");
      return MyString; // error returning local reference
}


So there you go - I hope you find all this useful <8+D

regards & cheers

Edit:

Hell, I forgot to say - Well done on your original code :+)
Last edited on
Topic archived. No new replies allowed.