need program help with the Constructor

Write a class named Employee that has the following member variables:

• name. A string that holds the employee’s name.
• idNumber. An int variable that holds the employee’s ID number.
• department. A string that holds the name of the department where the employee works.
• position. A string that holds the employee’s job title.

The class should have the following constructors:

• A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name, employee’s ID number, department, and position.
• A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name and ID number. The department and position fields should be assigned an empty string (“ “);
• A default constructor that assigns empty strings (“ “) to the name, department, and position member variables, and 0 to the idNumber member variable.

Write appropriate mutator functions that store values in these member variables and accessor functions that return the values in these member variables. Once you have written the class, create a three Employee objects (3 separate instances), in your main function, to hold the following data.



This is my code: It compiles, but can't get (Name,ID Number,Department,Position) to show up beside each entry!

EmployeeClass.h

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
99
100
101
102
103
104
105
ifndef Employee_Header
#define Employee_Header
#include <string>

using namespace std;

class Employee
{
private:
	string name;
	int idnumber;
	string department;
	string position;
public:
	Employee(string n, string id)
	{
		string name = "";
		string idnumber = "";
		string department = " ";
		string position = " ";
	}

	Employee(string n, int id, string dept, string pos)
	{
		name = n;
		idnumber = id;
		department = dept;
		position = pos;
	}

	void setName(string n)
	{
		name = n;
	}

	void setID(int id)
	{
		idnumber = id;
	}

	void setDEPT(string dept)
	{
		department = dept;
	}

	void setPOS(string pos)
	{
		position = pos;
	}

	string getName() const
	{
		return name;
	}

	int getID() const
	{
		return idnumber;
	}

	string getDEPT() const
	{
		return department;
	}

	string getPOS() const
	{
		return position;
	}

};

#endif 

Main.Cpp
#include "EmployeeClass.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	const int NUM_OBJECTS = 3;
	Employee employee[NUM_OBJECTS] = {
		Employee("Susan Meyers", 47899, "Accounting", "Vice President"),

		Employee("Mark Jones", 39119, "IT", "Programmer"),

		Employee("Roy Rogers", 81774, "Manufacturing", "Engineer")
	};

	for (int i = 0; i < NUM_OBJECTS; i++)
	{
		cout << employee[i].getName() << endl;
		cout << employee[i].getID() << endl;
		cout << employee[i].getDEPT() << endl;
		cout << employee[i].getPOS() << endl;
	}

	cin.ignore();
	cin.get();
	return 0;
}
Last edited on
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
#include <string>

// Do not, repeat DO NOT, place the directive using namespace std in a header file.

class Employee
{
    private:
        std::string name;
        int idnumber;
        std::string department;
        std::string position;

    public:
        // use a member initializer list to initialise the members
        // http://en.cppreference.com/w/cpp/language/initializer_list
        Employee( std::string n, int id ) : name(n), idnumber(id) {}

        Employee( std::string n, int id, std::string dept, std::string pos )
            : name(n), idnumber(id), department(dept), position(pos) {}

        std::string Name() const { return name ; }
        void Name( std::string n ) { name = n ; }

        int Id() const { return idnumber ; }
        void Id( int idnum ) { idnumber = idnum ; }

        std::string Dept() const { return department ; }
        void Dept( std::string d ) { department = d ; }

        std::string Pos() const { return position ; }
        void Pos( std::string p ) { position = p ; }
};

#include <iostream>

int main()
{
	const int NUM_OBJECTS = 3;
	Employee employee[NUM_OBJECTS] =
	{ // use a consistent brace style

		Employee("Susan Meyers", 47899, "Accounting", "Vice President"),

		Employee("Mark Jones", 39119, "IT", "Programmer"),

		Employee("Roy Rogers", 81774, "Manufacturing", "Engineer")
	};

	for( int i = 0; i < NUM_OBJECTS; ++i )
	{
		std::cout << employee[i].Name() << ' ' << employee[i].Id() << ' '
		          << employee[i].Dept() << ' ' << employee[i].Pos() << '\n' ;
	}

	std::cout << "----------------------------------\n" ;
	// favour a range-based loop over a classical for loop
	// http://www.stroustrup.com/C++11FAQ.html#for
	for( const auto& emp : employee )
	{
		std::cout << emp.Name() << ' ' << emp.Id() << ' '
		          << emp.Dept() << ' ' << emp.Pos() << '\n' ;
	}
}

http://coliru.stacked-crooked.com/a/71e10066da0f87b8
It compiles, but can't get (Name,ID Number,Department,Position) to show up beside each entry!

Do you mean something like this:
1
2
3
4
5
6
7
    for (int i = 0; i < NUM_OBJECTS; i++)
    {
        cout << "Name:       " << employee[i].getName() << endl;
        cout << "ID number:  " << employee[i].getID()   << endl;
        cout << "Department: " << employee[i].getDEPT() << endl;
        cout << "Position:   " << employee[i].getPOS()  << "\n" << endl;
    }


Other comments.

It is usually considered bad practice to put using namespace std; inside a header file. That's because it may silently change the behaviour of any program which includes that header. If you remove that, it does mean you will need to explicitly specify std::string where necessary. In the .cpp file you may still put using namespace std; if you wish.

The instructions call for three different constructors, but you only provide two of them. You should probably also test all of them in main() at least to your own satisfaction, regardless of what the problem instructs you to do. (you might later remove your test code from main() if felt necessary).

Also this constructor is broken in two separate ways:
1
2
3
4
5
6
7
	Employee(string n, string id)
	{
		string name = "";
		string idnumber = "";
		string department = " ";
		string position = " ";
	}

One, it declares local variables which no longer exist as soon as the end of the constructor is reached.
Two, it ignores both parameters string n, string id

Hello guys I changed my code a bit and now I'm getting these two errors: Can someone run the code and help me please;

1. Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall Employee::~Employee(void)" (??1Employee@@QAE@XZ) referenced in function "public: __thiscall Employee::Employee(void)" (??0Employee@@QAE@XZ) Employee c:\Users\freaktstud21\documents\visual studio 2017\Projects\Employee\Employee.obj 1

2. 1 unresolved externals

Employee.h

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
ifndef Employee_Header
#define Employee_Header
#include <string>

using namespace std;

class Employee
{
public:
	Employee();
	~Employee();

private:
	string name;
	int idnumber;
	string department;
	string position;
public:
	Employee(string n, int id)
	{
		name = n;
		id = idnumber;
		department = " ";
		position = " ";
	}

	Employee(string n, int id, string dept, string pos)
	{
		name = n;
		idnumber = id;
		department = dept;
		position = pos;
	}

	void setName(string n)
	{
		name = n;
	}

	void setID(int id)
	{
		idnumber = id;
	}

	void setDEPT(string dept)
	{
		department = dept;
	}

	void setPOS(string pos)
	{
		position = pos;
	}

	string getName() const
	{
		return name;
	}

	int getID() const
	{
		return idnumber;
	}

	string getDEPT() const
	{
		return department;
	}

	string getPOS() const
	{
		return position;
	}

};

#endif 


Employee.Cpp

#include "Employee.h"
#include <iostream>
#include <string>
#include <iomanip>

Employee::Employee()
{

	Employee::~Employee();
}
	
	using namespace std;

	int main()
	{

		const int NUM_OBJECTS = 3;
		Employee employee[NUM_OBJECTS] = {
			Employee("Susan Meyers", 47899, "Accounting", "Vice President"),
			Employee("Mark Jones", 39119, "IT", "Programmer"),
			Employee("Roy Rogers", 81774, "Manufacturing", "Engineer")
		};

		for (int i = 0; i < NUM_OBJECTS; i++)
		{
			cout << employee[i].getName();
			cout << employee[i].getID();
			cout << employee[i].getDEPT();
			cout << employee[i].getPOS() << endl;
	};


		cin.ignore();
		cin.get();
		return 0;
	}



If terms of organisation of the code, this:
1
2
3
4
5
Employee::Employee()
{

	Employee::~Employee();
}

doesn't belong in the main cpp file. To be consistent, put it together with the other constructors in the header file.

The compiler error message:
Error LNK2019 unresolved external symbol "public: __thiscall Employee::~Employee(void)" 

means that you have provided the declaration for the destructor (in two places, first in the header at line 11 in Employee.h
 
	~Employee();

and rather strangely it is declared again in Employee.Cpp at line 90 (or line 9)
 
	Employee::~Employee();

So the compiler reads the declaration and makes a note of it. Later the linker tries to find the implementation details for that destructor, but you didn't provide one.

All you need to do to fix this is to go to line 11 in Employee.h and change it to:
 
	~Employee() {  };

Add an empty pair of braces to indicate an actual implementation.

Then remove the other unnecessary declaration in Employee.cpp.

Other comments:
In this constructor the assignment is the wrong way around:
1
2
3
4
5
6
7
	Employee(string n, int id)
	{
		name = n;
		id = idnumber;
		department = " ";
		position = " ";
	}


And finally another mention that you should not put using namespace std; in the header file. It does mean you will have to put std::string in a few places in the header.


Hello Chervil Thanks for all your help; Now how do I fix these two parts? I'm getting a bit confused on the constructors that I missed!

******The instructions call for three different constructors, but you only provide two of them. You should probably also test all of them in main() at least to your own satisfaction, regardless of what the problem instructs you to do. (you might later remove your test code from main() if felt necessary).

Also this constructor is broken in two separate ways:

Employee(string n, string id)
{
string name = "";
string idnumber = "";
string department = " ";
string position = " ";
}

One, it declares local variables which no longer exist as soon as the end of the constructor is reached.
Two, it ignores both parameters string n, string id

Other comments:
In this constructor the assignment is the wrong way around:

1
2
3
4
5
6
7
Employee(string n, int id)
	{
		name = n;
		id = idnumber;
		department = " ";
		position = " ";
	}
Last edited on
Can I please get some help!
It's not clear what help you're asking for at this point.
Did you not understand chervil's response?

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
99
//  employee.h
#ifndef Employee_Header
#define Employee_Header
#include <string>

class Employee
{   std::string name;
	int idnumber;
	std::string department;
	std::string position;

public:
    //  Default constructor
	Employee ()
	{   idnumber = 0;
	    name = "";
	    department = "";
	    position = "";
    }	    
    //  2 element constructor
	Employee (std::string n, int id)
	{   name = n;
		//  id = idnumber;      This is backwards
		idnumber = id;
		department = "";
		position = "";
	}
	//  4 element constructor
	Employee (std::string n, int id, std::string dept, std::string pos)
	{   name = n;
		idnumber = id;
		department = dept;
		position = pos;
	}
   	~Employee ()
   	{}

	void setName (std::string n)
	{   name = n;
	}

	void setID (int id)
	{   idnumber = id;
	}

	void setDEPT (std::string dept)
	{   department = dept;
	}

	void setPOS (std::string pos)
	{   position = pos;
	}

	std::string getName () const
	{   return name;
	}

	int getID () const
	{   return idnumber;
	}

	std::string getDEPT () const
	{   return department;
	}

	std::string getPOS () const
	{   return position;
	}
};
#endif 


//  Employee.cpp
//  Not needed.  All member functions are defined in the header

//  main.cpp
#include "employee.h"       
#include <iostream>
using namespace std;

int main()
{   const int NUM_OBJECTS = 3;
	Employee employee[NUM_OBJECTS] = 
	{   Employee("Susan Meyers", 47899, "Accounting", "Vice President"),
		Employee("Mark Jones", 39119, "IT", "Programmer"),
		Employee("Roy Rogers", 81774, "Manufacturing", "Engineer")
	};

	for (int i = 0; i < NUM_OBJECTS; i++)
	{   cout << employee[i].getName() << " ";
		cout << employee[i].getID() << " ";
		cout << employee[i].getDEPT() << " ";
		cout << employee[i].getPOS() << endl;
	};

	cin.ignore();
	cin.get();
	return 0;
}
Susan Meyers 47899 Accounting Vice President
Mark Jones 39119 IT Programmer
Roy Rogers 81774 Manufacturing Engineer
Topic archived. No new replies allowed.