Multiple Constructors

I can't seem to understand why my the code only uses the first constructor. Please help!

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
 #include<iostream>
using namespace std;
#include <cstring>
const int DEFAULT = 81;
	class Employee
{
private:
	char employeeName[DEFAULT];
	int idNumber;
	char department[DEFAULT];
	char jobPosition[DEFAULT];

public:

	Employee()
	{
		strcpy (employeeName, "Susan Meyers");
		idNumber = 47899;
		strcpy (department, "Accounting");
		strcpy (jobPosition, "Vice President ");
	}

	Employee(char *employ, char *depart, char *job)
	{
		strcpy (employeeName, "Mark Jones");
		idNumber = 39119;
		strcpy (department, depart);
		strcpy (jobPosition, job);
	}
	
	Employee(char *employ, char *depart, char *job, int id )
	{
		strcpy (employeeName, employ);
		idNumber = id;
		strcpy (department, depart);
		strcpy (jobPosition, job);
	}
	
	
	void setDepartment(char *depart) 
	{ strcpy(department, depart); }

	void setName(char *employ)
	{ strcpy (employeeName, employ); }

	void setPosition(char *job)
	{ strcpy (jobPosition, job); }
	
	void setIdNumber(int id)
	{ idNumber = id; }

	char *getName()
	{ return employeeName; }

	char *getDepartment()
	{ return department; }

	char *getJobPosition()
	{ return jobPosition; }
	int getIdNumber()
	{ return idNumber;}
};

int main()
{
	// Create an Employee object to test constructor #1.
   Employee susan;

	// Create an Employee object to test constructor #2.
   Employee mark;
   mark.setDepartment("IT");
   mark.setPosition("Programmer");

   // Create an Employee object to test constructor #3.
   Employee joy;
   joy.setName("Joy Rogers");
   joy.setIdNumber(81774);
   joy.setDepartment("Manufacturing");
   joy.setPosition("Engineer");
   
   // Display each employee's data.
	cout << susan.getName() << endl;
	cout << susan.getDepartment() << endl;
	cout << susan.getJobPosition() << endl;
	cout << susan.getIdNumber() << endl << endl;
	
	cout << joy.getName() << endl;
	cout << joy.getDepartment() << endl;
	cout << joy.getJobPosition() << endl;
	cout << joy.getIdNumber() << endl << endl;

	cout << mark.getName() << endl;
	cout << mark.getDepartment() << endl;
	cout << mark.getJobPosition() << endl;
	cout << mark.getIdNumber() << endl << endl;
	
   system("pause");
   return 0;
Only one constructor will be used, it's up to you to choose it.

To use a different constructor, you do something like this, where the arguments types, and order match the parameters of the constructor you wish to call.

Employee dwight("Dwight K. Schrute", "sales", "salesman and assistant to the regional manager");

If you leave out an explicit call to the constructor, as in Employee dwight;, then the default constructor, the one with no parameters, is called implicitly.
Last edited on
Because you're only using the first constructor (or default constructor).

1
2
3
4
// Create an Employee object to test constructor #2.
Employee mark;
mark.setDepartment("IT");
mark.setPosition("Programmer");


Try this instead:

Employee mark("Mark Wahlberg", "IT", "Programmer");

Topic archived. No new replies allowed.