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 :)