#include "Employee.h"
usingnamespace std;
Employee::Employee()
{
}
// Function prototype
void displayEmployee(Employee);
// Driver program to demonstrate the class
int main()
{
// Create an Employee object to test constructor #1.
// Create susan
Employee susan;
// Create an Employee object to test constructor #2.
// Create mark
Employee mark;
// Create an Employee object to test constructor #3.
// Create joy
Employee joy;
// Display each employee's data.
cout << left << setw(20) << "Name" << setw(15) << "ID Number" << setw(15) << "Department" << setw(15) << "Position";
displayEmployee(susan);
displayEmployee(mark);
displayEmployee(joy);
return 0;
}
//**************************************************
// The displayEmployee function displays the data *
// in the Employee object passed as an argument. *
//**************************************************
void displayEmployee(Employee e)
{
}
// Employee Class Declaration
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
usingnamespace std;
class Employee {
private:
// Employee's name
string nameEmployee;
// ID number
int numberID;
// Department name
string nameDepartment;
// Employee's position
string position;
public:
// 3 Constructors <---------------------------------------------------------this?
Employee();
// 4 Mutators
void setEmployeeName();
void setIdNumber();
void setDepartmentName();
void setPosition();
// 4 Accessors
string getEmployeeName();
int getIdNumber();
string getDepartment();
string getPosition();
};
#endif
Yea I had just started writing in the header file when I came across the 3 constructors instructions in the comments. It confused me because I don't understand why I would really need three. I know about overloading constructors to make more, but I still don't get why I would need to do this in this case.
So I could set the first constructor to initialize everything. Then another for the setters and the other for the getters?
but I still don't get why I would need to do this in this case.
Because the instructions tell you to?
As CptJV pointed out, you would have different constructors to allow instantiating Employee with different combinations of data. CptJV gave you 3 examples.
1) A default constructor (line 4). No information supplied.
2) A constructor that takes only an employee name (line 5).
3) A constructor that takes both an employee name and an employee id (line 6).
So I could set the first constructor to initialize everything.
Yes, although the only thing you really need to initialize is numberID. Al the other members are strings which automatically default to empty strings.
Then another for the setters and the other for the getters?
No. Your getters and setters are unrelated to your constructors.
So should i use the setters in the constructors then? to make use of them.
You can, but it's not necessary. Your constructors have access to the member variables.
The only time I would call a setter from a constructor is if the setter has additional logic, such as editing the value.
Do i also have to initialize the id number to 0 if one of the constructors does not take that parameter?