Using a Class for an Employee Salary calculation problem

I'm having problems constructing this program that calculates and displays a new employee salary. It uses a class, the Employee class. I am not as familiar with classes as I am with other operations.

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
* Arooj Mohammed	C++	Ch14ConEx5

*/

//Employee.cpp – calculates and displays a new salary

#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

//declaration section
class Employee
{
public:
	//initializes varaibles
	Employee();
	//assigns program value to variables
	void AssignValue(string, float);
	//returns name
	string DisplayName();
	//returns salary
	double DisplaySalary();
	//calculates new salary
	double CalcNewSalary();
private:
	string name;
	double salary;
	double raiseRate;

};

//implementation sectoin
Employee::Employee()
{
	name = "";
	salary = 0;
	raiseRate = 0.0;

} //end of default constructor

void Employee::AssignValue(string n, double s, double r)
{
	name = n;
	salary = s;
	raiseRate = r;
} //end of AssignValue method

string Employee::DisplayName();
{
	return name;
}	//end of DisplayName method
double Employee::DisplaySalary();
{
	return salary;
}	//end of DisplaySalary() method
double Employee::CalcNewSalary()
{
	if (raiseRate >= 0)
	{
		salary = salary * raiseRate;
	}

	else
	{
		salary = 0.00;
	}//end if

	return salary;
}	//end of CalcNewSalary method

int main()
{	
	//create Employee object
	Employee hireEmployee;

	//declare variables
	string name = "";
	double pay  = 0;
	double rate = 0.0;

	//get name, salary, and raise percentage
	cout << "Employee's name: ";
	getline(cin, name);
	cout << "Employee's current salary: ";
	cin >> pay;
	cout << "Raise rate: ";
	cin >> rate;

	//assign name and salary to the Employee object
	hireEmployee.AssignValue(name, pay, rate);

	//use the Employee object to display the name and current salary
	cout << "Name: "
		<< hireEmployee.DisplayName() << endl;
	cout << "Salary: "
		<< hireEmployee.DisplaySalary() << endl;

	//use the Employee object to calculate the new salary
	 pay = hireEmployee.CalcNewSalary();

	//use the Employee object to display the new salary
	cout << pay << endl;
}   //end of main function 


And i get these errors:


Error 1 error C2511: 'void Employee::AssignValue(std::string,double,double)' : overloaded member function not found in 'Employee'

Error 2 error C2761: 'std::string Employee::DisplayName(void)' : member function redeclaration not allowed

Error 3 error C2447: '{' : missing function header (old-style formal list?)

Error 4 error C2761: 'double Employee::DisplaySalary(void)' : member function redeclaration not allowed

Error 5 error C2447: '{' : missing function header (old-style formal list?)

Error 6 error C2660: 'Employee::AssignValue' : function does not take 3 arguments


Can someone explain to me how to correct this code and how to use the class properly?

Thanks







The errors really give you all the info you need. Error1 - look at the declaration and definition of AssignValue - do they both take the same arguments (and return the same type of value)? Look at where you're using AssignValue; again, same argument types?

The DisplayName issue is a bit more subtle, so I'll give it to you. Often, people copy/paste a function name from the declaration:

string DisplayName();

to create the definition

1
2
3
4
string Employee::DisplayName();
{
	return name;
}	//end of DisplayName method 


However, the two big differences with the definition are that 1) the definition requires the class name prefixed to the method name and 2) the method definition does NOT need (or allow) a semi-colon at the end of the first line. Review all your method definitions, fix where appropriate, and several of your errors will go away.
Topic archived. No new replies allowed.