3 constructors?

How would i use 3 constructors in this class?
I can make it work with one, but i dont get why you would use three?



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
#include "Employee.h"
using namespace 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> 
using namespace 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 
Last edited on
You can create three constructors, but they need three different numbers of parameter.
The constructors differe by the number of parameters. For example :

1
2
3
4
5
6
7
8
9
...
public:    
	// 3 Constructors <---------------------------------------------------------this?
	Employee();
        Employee(string);
        Employee(string, int);

	// 4 Mutators
...


Then you need to implement each of them, as you did for the first one. For exemple :

1
2
3
4
5
Employee::Employee(string e, int i)
{
      nameEmployee = e;
      numberID = i;
}


You should have a look at your setters that don't have any parameters... it's not correct I think. Should be :

void setEmployeeName(String e);

where the implementation is :

1
2
3
4
void Employee::setEmployeeName(string e)
{
     nameEmployee = e;
}

Hope that helps ...



Last edited on
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.
Last edited on
mmmk. So should i use the setters in the constructors then? to make use of them.

Edit - Do i also have to initialize the id number to 0 if one of the constructors does not take that parameter?
Last edited on
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?

Yes.
Topic archived. No new replies allowed.