enter employee info using class

Write the code that declares a class called Employee. Provide the following functionality:
a. Data members: name, age, yearsOfService and salary .
b. Accessor and mutator functions are required for each of the data members.
c. Write a program with the Employee class that makes (instantiates) two employees using the default constructor and sets their personal data.
NOTE: the default constructor should call the mutator functions to ensure that all data is input.
d. In main() print the data for both employees, along with nice messages.
e. In main() the program should then compare the salaries of the two employees and print the name of the employee who earns the higher salary, along with a nice message.

i think im missing a lot of things. can you guys please help me. thank u

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
int main () 
{
	class Emplyee
{
	private:
	char name;
	int age;
	double yearsOfService;
	double salary;
	public:
	void setYearsOfService (double);
	void setSalary (double);
	double getYearsOfService ();
	double getSalary ();
};
	void Employee: :setYearsOfService( double y)
{
	yearsofservice = 10;
}
	void Employee: :setSalary ( double s)
{
	salary = 500;
}
	double Employee: :getYearsOfService()
{
	return yearsofservice;
}
	double Employee: :getSalary()
{
	return salary;
}

}
You're more likely to get a faster, more useful response if you ask about a specific part of your assignment, and not just a general "help".

Some things, in no particular order:
- You do not define a default contructor, so the compiler generates one for you (which does not initialize anything). You're supposed call your mutator ("set") functions from within your constructor.
- Notice that you have 'y' as a parameter to your "setYearsOfService" function, but you never use it. Instead, you hardcode it to always set yearsofservice as '10'. Same issue with 'setSalary'.
- Capitalization matters in C++. yearsOfService is not the same as yearsofservice.
- A char is not a string. A char is a single letter. Use a string instead.
1
2
3
4
5
6
7
#include <string>
// ...
class Employee {
    ...
    std::string name;
    ...
};

- The "::" (scope resolution) operator needs to be "::", not ": :" (no spaces).

As you write code, you should be attempting to compile/build it often. This will help you catch syntax or semantic errors early on, and not overwhelm you later on.
Last edited on
This is a typical skeleton class for getters/setters:

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
#include <string>
#include <iostream>

class Employee {
private:
	std::string name;
	int age {};
	double yearsOfService {};
	double salary {};

public:
	Employee() {}
	Employee(const std::string& n, int a, double y, double s) : name(n), age(a), yearsOfService(y), salary(s) {}

	void setYearsOfService(double y) { yearsOfService = y; }
	void setSalary(double s) { salary = s; }
	void setName(std::string& n) { name = n; }
	double getYearsOfService() const { return yearsOfService; }
	double getSalary() const { return salary; }
	std::string getName() const { return name; }
};

int main() {
	Employee e1;
	Employee e2("emp2", 23, 2, 24000);
}

Topic archived. No new replies allowed.