Employee class

sorry for the lengthy post but i wanted to make it clear what i am supposed to be doing

Write a class named Employee that has the following member variables:
•name: The name attribute holds an employee’s first and last name
•idNumber: The idNumber attribute is a string variable that holds an employee’s ID number
•Department: The department attribute holds the name of the employee’s department
•position: The position attribute holds the name of the employee’s job title
•yearsWorked: This attribute holds the number of years the employee has worked at the company
•A constructor that accepts the following values as arguments and assigns them to the appropriate data members (attributes): employee’s name and employee’s ID number, employee’s department, employee’s position, and the number of years the employee has worked at the company.
•A constructor that accepts the following values as arguments and assigns them to the appropriate data members (attributes): employee’s name and employee’s ID number. The department and position attributes should be initialized to the empty string (“”) and the yearsWorked attribute should be initialized to zero.
•A default constructor (accepts no arguments) that initializes the name, idNumber, department, and position attributes to the empty string (“”). The yearsWorked attribute should be initialized to zero.

Write Get and Set methods for each attribute: name, idNumber, department, position, and yearsWorked.
Do not allow the yearsWorked attribute to be set to values less than zero. If an attempt is made to set yearsWorked to less than zero, do not set the attribute and communicate the problem to the calling program.
Remember that the class declaration (.h file) will contain the class attributes, function prototypes, and the function definitions.
Name the classdeclaration file employee.h.Next create a program to utilize the Employee class you created in the following manner:
•Declare 3 Employee objectso
One with all 5 args includedo
One with only the first two args includedo
One with no args.
Use set functions to set the missing values of the partially constructed objects.
Demonstrate the error detection of the setyearsWorked function.

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
  // this is the .h file
#include <string>
#include <iostream>
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

using namespace std;

class employee
{
private:
	string name;
	string idNumber;
	string department;
	string position;
	int yearsWorked;

public:
	employee(string n, string idNum, string depart, string pos, int yrsWrkd)
	{
		name = n;
		idNumber = idNum;
		department = depart;
		position = pos;
		yearsWorked = yrsWrkd;
	}
	employee(string n, string idNum)
	{
		name = n;
		idNumber = idNum;
		department = " ";
		position = " ";
		yearsWorked = 0;
	}
	employee()
	{
		name = "";
		idNumber = "";
		department = "";
		position = "";
		yearsWorked = 0;
	}

	void setName(string n)
	{
		name = n;
	}
	void setIdnumber(string idNum)
	{
		idNumber = idNum;
	}
	void setDepartment(string depart)
	{
		department = depart;
	}
	void setPos(string pos)
	{
		position = pos;
	}
	void setYEARS(int yrsWrkd)
	{
		yearsWorked = yrsWrkd;
		if (yearsWorked < 0)
		{
			yearsWorked = 0;
		}
	}

	string const getName()
	{
		return name;
	}
	string const getID()
	{
		return idNumber;
	}
	string const getDPT()
	{
		return department;
	}
	string const getPOS()
	{
		return position;
	}
	int const getYEARS()
	{
		return yearsWorked;
	}
};

#endif 


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
// this is the main cpp file
#include "employee.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{

	const int NUM = 4;
	employee emp[NUM] = {
		employee("Jenny Jacobs", "JJ8990", "Accounting", "President", 15),
		employee("Myron Smith", "MS7571", "IT", "Programmer", 5),
		employee("Chris Raines", "CR6873", "Manufacturing", "Engineer", 30)
	};

	emp[1].setYEARS(-1);

	for (int i = 0; i < NUM; i++)
	{
		cout << fixed << setw(14) << emp[i].getName() << setw(14) << emp[i].getID() << setw(14) << emp[i].getDPT() << setw(14) << emp[i].getPOS() << setw(14) << emp[i].getYEARS() << endl;
	}

	return 0;
}


code is working although it is not doing what i intended. the years for "Myron Smith" are supposed to revert to 5. And a the message "attempt to set yearsWorked for XXXX was invalid. It was set to 0".

thank you, I understand no one is supposed to do my homework for me :)
Last edited on
the years for "Myron Smith" are supposed to revert to 5.

But you set it to zero. Here, you call this:
emp[1].setYEARS(-1);

which calls this:

1
2
3
4
5
6
7
8
void setYEARS(int yrsWrkd)
	{
		yearsWorked = yrsWrkd;
		if (yearsWorked < 0)
		{
			yearsWorked = 0;
		}
	}


and as you can clearly see, the yearsWorked gets set to -1 straight away, and then you set it to zero.

Let's read the instructions again:
If an attempt is made to set yearsWorked to less than zero, do not set the attribute and communicate the problem to the calling program.


What does your funciton actually do? Your function sets yearsWorked to whatever number was passed in, and then might set it to zero. That's not right, is it? Do what the instructions say. The function should look like this:

1
2
3
4
5
6
7
8
9
10
11
void setYEARS(int yrsWrkd)
	{
            if (yrsWrked < 0) // IF an attempt is made to set yearsWorked to less than zero...
            {
                // here, DO NOT SET and communicate the problem
            }
            else
            {
                // here, DO set the value
           }
}


You just have to do what the instructions say.
Last edited on
Hey thanks for your reply, I have been trying to figure out how to not communicate the problem using a flag but i cannot get it to do it.

Is there a special way to not have it set the value and use go back to the default value?

I am able to get it to print out what is in the array now but the message is not printing out anymore.

1
2
3
4
5
6
7
8
9
10
11
	void setYEARS(int yrsWrkd)
	{
		
		if (yearsWorked < 0)
		{
			cout << "attempt to set yearsWorked for " << name << " was invalid. It was set to 0" << endl;
			yrsWrkd = yearsWorked;
		}
		else
			yrsWrkd = yrsWrkd;
	}


I am also not sure about this part of the instructions:

Next create a program to utilize the Employee class you created in the following manner:
•Declare 3 Employee objects
One with all 5 args included
One with only the first two args included
One with no args.
Use set functions to set the missing values of the partially constructed objects.

haven't I done this in the .h file? or am i supposed to call the the functions into the .cpp file?

this is my first project using classes :D
Last edited on
1
2
3
4
5
6
7
8
9
10
11
void setYEARS(int yrsWrkd)
	{
		
		if (yearsWorked < 0) // WHY ARE YOU COMPARING THE VALUE ALREADY SET?
		{
			cout << "attempt to set yearsWorked for " << name << " was invalid. It was set to 0" << endl; // THIS ISN'T TRUE. YOU HAVEN'T SET ANYTHING TO ZERO. DO NOT LIE TO THE USER.
			yrsWrkd = yearsWorked; // THIS IS POINTLESS. WHAT ARE YOU TRYING TO DO? THE MEMBER VARIABLE is yearsWorked
		}
		else
			yrsWrkd = yrsWrkd; // THIS MAKES NO SENSE AT ALL. YOU'RE SETTING yrsWrkd TO THE VALUE yrsWrkd? 
	}


Is there a special way to not have it set the value and use go back to the default value?

Here is how to have it NOT set the value:
1
2
3
{

}

You just do nothing.
Last edited on
heh. ^^

nothing 'special'
but you probably are asking for something in this form:

1
2
3
4
5
6
7
void foo(int input)
{
    if(input = valid)
       member = input;
    else    //comment out these two lines to do nothing at all if you prefer that. 
       member = defaultinitialvalue;
}

There is a time and a place for this. Personally I like to fuss until the user gives me something I can use, and then I update it, but not all users are human. If you are trying to talk to a computer that just got unplugged, this sort of thing has merits in some situations. in other code, it can be better to just gracefully give up, throwing an error. It depends on the situation -- as an example we used to check sensor data for anomalies and would revert to previous value if we got something too improbable. If the improbable data came back repeatedly we would eventually accept it (the idea was to filter noise, not data), causing a ms or two of lag due to the filter, but more often than not, it was bad data.
Last edited on
I understand now here is what I have,

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
// .h file
#include <string>
#include <iostream>
#ifndef EMPLOYEE_H
#define EMPLOYEE_H

using namespace std;

class employee
{
private:
	string name;
	string idNumber;
	string department;
	string position;
	int yearsWorked;
	bool message = false;

public:
	employee(string n, string idNum, string depart, string pos, int yrsWrkd)
	{
		name = n;
		idNumber = idNum;
		department = depart;
		position = pos;
		yearsWorked = yrsWrkd;
	}
	employee(string n, string idNum)
	{
		name = n;
		idNumber = idNum;
		department = " ";
		position = " ";
		yearsWorked = 0;
	}
	employee()
	{
		name = "";
		idNumber = "";
		department = "";
		position = "";
		yearsWorked = 0;
	}

	void setName(string n)
	{
		name = n;
	}
	void setIdnumber(string idNum)
	{
		idNumber = idNum;
	}
	void setDepartment(string depart)
	{
		department = depart;
	}
	void setPos(string pos)
	{
		position = pos;
	}
	void setYEARS(int yrsWrkd)
	{
		
		if (yrsWrkd < 0)
		{
			cout << "attempt to set yearsWorked for " << name << " was invalid. It was set to 0" << endl;
		}
		else
			yearsWorked = yrsWrkd;
	}

	string const getName()
	{
		return name;
	}
	string const getID()
	{
		return idNumber;
	}
	string const getDPT()
	{
		return department;
	}
	string const getPOS()
	{
		return position;
	}
	int const getYEARS()
	{
		return yearsWorked;
	}
};

#endif 


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
// .cpp file
#include "employee.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{

	const int NUM = 4;
	employee emp[NUM] = {
		employee("Jenny Jacobs", "JJ8990", "Accounting", "President", 15),
		employee("Myron Smith", "MS7571", "IT", "Programmer", 5),
		employee("Chris Raines", "CR6873", "Manufacturing", "Engineer", 30)
	};

	emp[1].setYEARS(-1);

	for (int i = 0; i < NUM; i++)
	{
		cout << fixed << setw(14) << emp[i].getName() << setw(14) << emp[i].getID() << setw(14) << emp[i].getDPT() << setw(14) << emp[i].getPOS() << setw(14) << emp[i].getYEARS() << endl;
	}

	return 0;
}


as to the "It was set to zero", my professor has that in his sample run of the problem. I am not sure why it says that even though he tells us to use emp[1].setYEARS(-1). Would you recommend fixing this? or leave as is due to my professor having that in his sample run.

here is his sample run
https://ibb.co/zZ6dZwd
Last edited on
Lying to the user - telling the user that you set something to zero when you didn't - sure doesn't seem like the right thing to do, but if that's what you're being taught to do, we might as well recognise that the grades are given out for doing what you're told.

int const getYEARS() and others like it. This doesn't seem right. That const isn't doing anything useful.

int getYEARS() const , on the other hand, makes a lot more sense.
Last edited on
I removed "It was set to zero" and ill make note of it to my professor in hopes he will understand haha.

i fixed the const issues as i looked up why it makes sense to have it after the functions.

the only problem i am having with it now is i am getting an extra 0 printed out at the end of the program. I believe it has something to do with my for loop but I cant figure it out.

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
#include "employee.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{

	const int NUM = 4;
	employee emp[NUM] = {
		employee("Jenny Jacobs", "JJ8990", "Accounting", "President", 15),
		employee("Myron Smith", "MS7571", "IT", "Programmer", 5),
		employee("Chris Raines", "CR6873", "Manufacturing", "Engineer", 30)
	};

	emp[1].setYEARS(-1);

	cout << setw(16) << left << "Name";
	cout << setw(16) << "ID Number";
	cout << setw(16) << "Department";
	cout << setw(16)<< "Position";
	cout << setw(16)<< "Years Worked" << endl;

	cout << setw(16) << left << "----";
	cout << setw(16) << "---------";
	cout << setw(16) << "----------";
	cout << setw(16) << "--------";
	cout << setw(16) << "------------" << endl;

	for (int i = 0; i < NUM; i++) // THINK THE PROBLEM IS IN HERE
	{
		cout << fixed << setw(16) << left << emp[i].getName() << setw(16) << emp[i].getID() << setw(16) << emp[i].getDPT() << setw(16) << emp[i].getPOS() << setw(5) << right << emp[i].getYEARS() << endl;
	}

	return 0;
}


this is what it prints out
attempt to set yearsWorked for Myron Smith was invalid.
Name            ID Number       Department      Position        Years Worked
----            ---------       ----------      --------        ------------
Jenny Jacobs    JJ8990          Accounting      President          15
Myron Smith     MS7571          IT              Programmer          5
Chris Raines    CR6873          Manufacturing   Engineer           30
                                                                    0<--This zero keeps on printing
Last edited on
Topic archived. No new replies allowed.