Overloaded setter/mutator

I am working on a school project where I am to create 2 instances of an employee object, one where we are to enter the information and one using provided values. In the program, I need to use an overloaded 'setter' to accept strings for two attributes- dependents and salary- instead of using the setters that take integer and double values. My problem is that my program compiles correctly, but when I enter a number, the program uses my default values instead. I have only made changes to the dependents function and not the salary one, so I know that function is still running the double-based setter and not the string one. At this point (after multiple alterations to the code), I'm not sure if I've named a variable incorrectly, if/how I'm missing a conversion from a string to an integer in my input portion of my code, or how I'm supposed to call the overloaded (string) setDependent function in my main. If someone could take a look at my code and shed some light on what I'm doing wrong and how to fix it, I'd appreciate it.


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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const double minSalary = 10000;
const double maxSalary = 300000;
const int maxDependents = 99;
const int minDependents = 0;
const char defaultGender = 'Z';


//Function prototypes
void DisplayApplicationInformation();
void DisplayDivider(string);
void DisplayDivider2(string);
void TerminateApplication();
string GetInput(string);

//Class definition
class Employee
{
public:
	Employee();
	Employee(string, string, char, int, double);
	~Employee();

	//public methods that access private attributes
	double calculatePay();
	void displayEmployee();

	//getters and setters
	string getFirstName();
	void setFirstName(string);
	string getLastName();
	void setLastName(string);
	char getGender();
	void setGender(char);
	int getDependents();
	void setDependents(int);
	void setDependents(string);
	double getAnnualSalary();
	void setAnnualSalary(double);
	void setAnnualSalary(string);
	static int getNumEmployees();
	
private:
	string firstName;
	string lastName;
	char gender;
	int dependents;
	double annualSalary;
	static int numEmployees;
};

int main()
{
	//variable declarations
	string tempString;
	string message;
	int dep = 0;
	char gen;
	double annSal;

	DisplayApplicationInformation();//starts the program
	DisplayDivider("Employee 1");

	
	Employee employee1;
	
	//This 11-line block of code gets all of the input for the object employee1.
	employee1.setFirstName(GetInput("first name: "));
	employee1.setLastName(GetInput("last name: "));
	
	tempString = GetInput("gender: ");
    gen = tempString.c_str()[0];
	employee1.setGender(gen);

    tempString = GetInput("number of dependents: ");
    getline(cin, tempString, '\n');
    employee1.setDependents(dep);

    tempString = GetInput("annual salary: ");
    annSal = atof(tempString.c_str());
    employee1.setAnnualSalary(annSal);

	DisplayDivider2("\nEmployee Information");
	employee1.displayEmployee();//This displays all of the information gathered in the above code.
	cout << endl;

	Employee employee2("Mary", "Noia", 'F', 5, 24000.0);
	DisplayDivider("Employee 2");
	DisplayDivider2("\nEmployee Information");
	employee2.displayEmployee();

	TerminateApplication();//the good bye message
	employee1.~Employee();
	employee2.~Employee();
	system("pause");
	return 0;
}

int Employee::numEmployees = 0;

Employee::Employee()
{
	firstName = "not given";
	lastName = "not given";
	gender = defaultGender;
	dependents = minDependents;
	annualSalary = minSalary;
	numEmployees++;
}

Employee::Employee(string first, string last, char gend, int deps, double salary)
{
	//initialize a whole bunch of variables in here
	firstName = first;
	lastName = last;
	
	//Gender, dependents and annual salary need input validation.
	if ((toupper(gend) == 'M') || (toupper(gend) == 'F'))
		gender = gend;
	else
	{
		cout << "You have entered an invalid input, default values will be used." << endl;
		gender = defaultGender;
	}

	if ((deps >= 0) && (deps <=99))
		dependents = deps;
	else
	{
		cout << "You have entered an invalid input, default values will be used." << endl;
		dependents = minDependents;
	}

	if ((salary >= minSalary) && (salary <= maxSalary))
		annualSalary = salary;
	else
	{
		cout << "You have entered an invalid input, default values will be used." << endl;
		salary = minSalary;
	}
	numEmployees++;
}

Employee::~Employee()
{
	numEmployees--;
}

double Employee::calculatePay()
{
	//code to calculate pay and return a double value here. To be used to calculate weekly salary.
	return annualSalary/52.0;
}

void Employee::displayEmployee()
{
	//code to display employee information.
	cout << "\n__________________________________________" << endl;
	cout << "Name:           " << firstName << " " << lastName << endl;
	cout << "Gender:         " << gender << endl;
	cout << "Dependents:     " << dependents << endl;
	cout << "Annual Salary:  $" << setprecision(2) << showpoint << fixed << annualSalary << endl;
	cout << "Weekly Salary:  $" << calculatePay() << endl;
	cout << "--- Number of Employee Objects Created----" << endl;
	cout << "Number of employees: " << Employee::getNumEmployees() << "\n" << endl;
}

//Getter and Setter definitions here. I put all of the setters with their corresponding getters.
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 gend)
{
	if ((toupper(gend) == 'M') || (toupper(gend) == 'F'))
		gender = gend;
	else
	{
		cout << "You have entered an invalid input, default values will be used." << endl;
		gender = defaultGender;
	}
}

int Employee::getDependents()
{
	return dependents;
}

void Employee::setDependents(int deps)
{
	if ((deps >= 0) && (deps <=99))
		dependents = deps;
	else
	{
		cout << "You have entered an invalid input, default values will be used." << endl;
		dependents = minDependents;
	}
}

void Employee::setDependents(string deps)
{
	int d = 1;
	string tempString;

	if ((d >= 0) && (d <= 99))
		dependents = tempString.at(0);
	else
	{
		cout << "You have entered an invalid input, default values will be used." << endl;
		dependents = minDependents;
	}
}

double Employee::getAnnualSalary()
{
	return annualSalary;
}

void Employee::setAnnualSalary(double salary)
{
	if ((salary >= minSalary) && (salary <= maxSalary))
		annualSalary = salary;
	else
	{
		cout << "You have entered an invalid input, default values will be used." << endl;
		salary = minSalary;
	}
}

void Employee::setAnnualSalary(string salary)
{
	double ts;

	ts = atof(salary.c_str());
	if ((ts >= minSalary) && (ts <= maxSalary))
	annualSalary = ts;
	else
	{
		cout << "You have entered an invalid input, default values will be used." << endl;
		annualSalary = minSalary;
	}
}

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

void DisplayApplicationInformation()// This is the header display shown to the end-user in the program
{
	cout << "Welcome to your first Object-Oriented program- The Employee Class" << endl;
    cout << "CIS247, Week 3 Lab" << endl;
    cout << "Name:  " << endl;
    cout << "This program will create 2 instances of the employee" << endl;
	cout << "class and display them to the screen." << endl;
	cout << "\n\nWhen prompted, please enter valid input." << endl;
	cout << "Otherwise, default values will be used instead." << endl;
}

void TerminateApplication() // This is the message shown before the program terminates
{
	cout << "\nThis is the end of the CIS247C Week 3 iLab.\n" << endl;

	return;
}

void DisplayDivider(string message)// This will show as a divider between input sections
{
    cout << "\n****************" << message << "****************" << endl;
}

void DisplayDivider2(string message)//This will show the 'employee information' divider.
{
	cout << message << endl;
}

string GetInput(string message)// This will put whatever is needed into the section breaks and is interchangeable
{
    string mystring;
    cout << "\nPlease enter your " << message;
    getline(cin, mystring, '\n');

    return mystring;
}
Last edited on
bump
Honestly, your explanation is close to not understandable. Be more precise and use terms from your code. Such as 'setAnnualSalary()'

You don't have anything overloaded. There're just more than one version of some of your functions.

Which version of the functions is called depends on the parameter the function is called with.

Searching for e.g. 'setAnnualSalary' shows that it is called on line 86 with the paramter from line 64 (double annSal). So the version that accepts double is called (and not the version that accepts string).
So if you want that 'setAnnualSalary(string)' is called then change the type on line 64 from double annSal to string annSal. The treatment of that variable must be changed too (of course)

Do this for all the others too.
Topic archived. No new replies allowed.