Constructor help

Pages: 12
How to i set the name of the employee? I dont understand how to use the string. The book only shows examples of integers. This is the assignment.

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.


This is my code:

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
#include "employee.h"
#include <iostream>
#include <string> 
using namespace std; 

//****************************************************
//constructor initliaizing everything to null        *
//****************************************************

Employee::Employee()
{
	name = "";
	position = "";
	department = "";
	idNumber = 0; 
}

//*********************************************
//setName sets member name                    *
//*********************************************

void setName 
{

}

Last edited on
This is my employee.h file to go with the main program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Employee class implementation file
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>

//Employee Declaration 

class Employee
{
private:
	std::string name;
	int idNumber;
	std::string department;
	std::string position;
public:
	Employee(); //constructor
	int getIdNum() const;
	void setName(std::string);
	void setDepartment(std::string);
	void setPostion(std::string);
};
#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
28
29
30
31
32
#include <iostream>
#include <string>

class Employee
{
private:
	std::string name;
	int idNumber;
	std::string department;
	std::string position;
public:
	Employee(std::string,std::string,std::string,int); //constructor
	int getIdNum() const;
	void setName(std::string);
	void setDepartment(std::string);
	void setPostion(std::string);
};


Employee::Employee(std::string empName, std::string empPos, std::string empDep, int idNum)
{
	empName = name;
	empPos  = position;
	empDep  = department;
	idNum   = idNumber;
}

int main()
{
    Employee employee1("Bill","manager","packaging",45);
    return 0;
}

Last edited on
Following your code, would i still need the void member functions? And where would i change the values of everything to null or 0? Thanks for taking the time to help me! I appreciate it. And also the only way im allowed to access the private part of class is through an accessor or class mutator. Did i do that correctly?
Last edited on
Although I have done some C programming I am not up to speed with OOP yet myself.

Actually I have just noticed your constructor is not assigning the data correctly it is back the front.

Yes you will need methods to set or get the private data of the class.

empName for example is a private member and can only be changed or accessed via a class method. So I have added the method getName() as an example of how to get the data. Methods must also be written to set the data members.

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
#include <iostream>
#include <string>
//using namespace std;

class Employee
{
private:
	std::string name;
	int idNumber;
	std::string department;
	std::string position;
public:
	Employee(std::string,std::string,std::string,int); //constructor
	int getIdNum() const;
	std::string getName();
	void setName(std::string);
	void setDepartment(std::string);
	void setPostion(std::string);
};


Employee::Employee(std::string empName, std::string empPos, std::string empDep, int idNum)
{
	name = empName;
	position = empPos;
	department = empDep;
	idNumber = idNum;
}

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

int main()
{
    Employee employee1("Bill","manager","packaging",45);
    std::cout << employee1.getName();
    return 0;
}

Im just starting to learn C so im not too familiar myself. Will this also work?

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
#include "employee.h"
#include <iostream>
#include <string> 
using namespace std; 

//****************************************************
//constructor initliaizing everything to null        *
//****************************************************

Employee::Employee()
{
	name = "";
	position = "";
	department = "";
	idNumber = 0; 
}

//*********************************************
//setName sets member employee name           *
//*********************************************

void Employee::setName(string empName)
{
	name = empName;
}

//********************************************
//setPositon to set the employee position    *
//********************************************

void Employee::setPostion(string empPos)
{
	position = empPos;
}

//********************************************************
//setDepartment to set what the employees dapartment is  *
//********************************************************

void Employee::setDepartment(string empDepart)
{
	department = empDepart;
}

//**********************************************
//getIdNum to find out what the id number is   *
//**********************************************

int Employee::getIdNum() const
{
	return idNumber;
}




I think what i did was basically write out every step while you had it nice and condensed
Last edited on
closed account (E0p9LyTq)
@kingkush,

Your class is halfway there. You need to declare and define a few more getters and setters.

You can retrieve idNumber already with your getter: int getIdNum() const;, just create a setter that lets you change the value of an already constructed object: void setIdNum(int id);.

You have 3 setters that allow you to change the value of your 3 string data members, simply add 3 setter methods.

You have your default constructor defined, that is good. Add in the parameterized constructor CodeWriter gave you. Then you can create an object with default data members or one with specified data members.
I apologize im just learning C and dont understand all the terms yet. Are getter and setters accessor and mutators? is this the parameter i should add? Employee(std::string,std::string,std::string,int); //constructor in the public part of my .h file? Im really confused because if i do that i get all these errors:

Error 2 error C2597: illegal reference to non-static member 'Employee::name'

but for each member.
Last edited on
You are learning c++ with classes not c which doesn't use classes. It is what they call object orientated programming. You need to spend more time on the basics such as understanding what terms like static or const actually mean.

You can have more than one constructor such as the one you wrote without parameters.
This is called overloading the method or function. So if you create an Employee object without parameters it will use that one instead of the other one.

But really learning this way on the forum is I think not a good way to learn. It is better to get a good book on c++ and work your way through it and all these things will be covered.
Last edited on
C/C libraries can still be object oriented.

 
Error 2 error C2597: illegal reference to non-static member 'Employee::name' 

If you're working inside the class itself, just use "name", not "Employee::name". If that error is from outside of the class, note that a user cannot access a variable like:
1
2
3
4
int main()
{
    std::string name = Employee::name; // bad, unless "name" is static
}

They must first make an instance of Employee:
1
2
3
4
5
6
7
int main()
{
    Employee mike;
    mike.name = "Michael";
    // or if you want private members:
    mike.setName("Michael");
}
Last edited on
Im actually do have the book but im not completely understanding it. I missed a day of class because i was sick which is why i dont understand this chapter as much. but is my .h file better now?

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
//Employee class implementation file
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>

//Employee Declaration 

class Employee
{
private:
	std::string name;
	int idNumber;
	std::string department;
	std::string position;
public:
	Employee();
	int getIdNum() const;
	void setIdNum(int id);
	void getName();
	void setName(std::string);
	void getDepartment();
	void setDepartment(std::string);
	void getPosition();
	void setPostion(std::string);
	
};
#endif  
Must confess at this stage I only use one file :)
I notice that the practice is to implement each class definition in its own .h file.
I would suggest your get methods should return a value and thus void doesn't cut it.
1
2
3
4
	int getIdNum() const;
	std::string getName();
	std::string getDepartment();
	std::string getPosition();


Last edited on
ohh i see. This is my first assignment where i needed to split my program into two files which is making it harder for me to understand. I know the functions shouldnt be void but if i change it to anything i start to get a lot of errors
When you update your "interface" to the code (the header file), you have to update the implementation to the code as well (the .cpp file).
In other words, make sure the function declarations in the header file match the implementations of the functions in cpp file, that they are also returning std::string instead of void.
so i changed void to string because they are returning words. Is this correct now? I have yet to still implement them into my .cpp file

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
//Employee class implementation file
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>

//Employee Declaration 

class Employee
{
private:
	std::string name;
	int idNumber;
	std::string department;
	std::string position;
public:
	Employee();
	int getIdNum() const;
	void setIdNum(int id);
	std::string getName();
	void setName(std::string);
	std::string getDepartment();
	void setDepartment(std::string);
	std::string getPosition();
	void setPostion(std::string);
	
};
#endif  
How would i do the getName function? this is what i have done so far.

1
2
3
4
5
6
7
8
9
//**********************************************
//getName to find out the employees name       *
//**********************************************
std::string getName(string empName)
{
	cout << " What is the employee's name? " << endl;
	cin >> empName;
	return empName;  
}


I feel like this is the wrong way to implement it but how would the proper way to implement this be?
Last edited on

You do it the way I have done above. Getting data may not involve the user.
Encapsulating data means the class methods have control over what can or cannot be done with that data.

1
2
3
4
5
6
7
//**********************************************
//getName to find out the employees name       *
//**********************************************
std::string getName()
{
	return empName;  
}



Last edited on
so would getDepartment() be written like this?

1
2
3
4
std::string getDepartment()
{
           return empDepartment;
}


And also can you explain what you mean by getting data may not involve the user? i feel this is what is confusing me.
i changed my code to this.

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
#include "employee.h"
#include <iostream>
#include <string> 
using namespace std; 

//****************************************************
//constructor initliaizing everything to null        *
//****************************************************

Employee::Employee()
{
	name = "";
	position = "";
	department = "";
	idNumber = 0; 
}

//*********************************************
//setName sets member employee name           *
//*********************************************

void Employee::setName(string empName)
{
	name = empName;
}

//**********************************************
//getName to find out the employees name       *
//**********************************************

std::string getName()
{
	return empName; 
}

//********************************************
//setPositon to set the employee position    *
//********************************************

void Employee::setPostion(string empPos)
{
	position = empPos;
}

//********************************************
//getPosition to get employee's position     *
//********************************************

std::string getPosition(string empPos)
{
	return empPos; 
}

//********************************************************
//setDepartment to set what the employees dapartment is  *
//********************************************************

void Employee::setDepartment(string empDepart)
{
	department = empDepart;
}

//*********************************************************
//getDepartment to get what the employee's department     *
//*********************************************************

std::string getDepartment()
{
	return department;
}

//***************************************************
//setIdNum to set the ID number                     *
//***************************************************
void Employee::setIdNum(int idNum)
{
	idNum = idNumber; 
}

//**********************************************
//getIdNum to find out what the id number is   *
//**********************************************

int Employee::getIdNum() const
{
	return idNumber;
}

int main()
{
	


}


Does this look more correct? I have two errors saying empName and department are not defined.
The method is not getting the data from the user it is getting it from the private member in the object the value of which is set by another method. Private data is accessed via public methods.

For some reason when I use your constructor which does not initialize the private variables I get an error on line 56
error: request for member 'setName' in 'employee1', which is of non-class type 'Employee'
However this version works:
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
#include <iostream>
#include <string>
//using namespace std;

class Employee
{
private:
	std::string name;
	int idNumber;
	std::string department;
	std::string position;
public:
	Employee(std::string,std::string,std::string,int); //constructor
	Employee();  //constructor with default settings
	int getIdNum() const;
	std::string getName();
	void setName(std::string);
	void setDepartment(std::string);
	void setPostion(std::string);
};


Employee::Employee(std::string empName, std::string empPos, std::string empDep, int idNum)
{
	name = empName;
	position = empPos;
	department = empDep;
	idNumber = idNum;
}

Employee::Employee()
{
	name = "";
	position = "";
	department = "";
	idNumber = 0;
}

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

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

int main()
{
    std::string aName = "";
    Employee employee1("Bill","manager","packaging",45);
    // Employee employee1();  // causes an error for some reason
    std::cout << "Enter a name ";
    std::cin  >> aName;
    employee1.setName(aName);
    std::cout << employee1.getName();
    return 0;
}

Pages: 12