C++ Employee class with getters and setters giving errors

  • Context: Comp Sci 
  • Thread starter Thread starter proton
  • Start date Start date
  • Tags Tags
    C++ Homework
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
4 replies · 2K views
proton
Messages
349
Reaction score
0
Since I don't know how to use the code on physics forums, I'll just type the problem I'm working on

I'm supposed to write a program with the class Employee that makes two employees; sets their age, yearsOfService, and Salary; and prints their values

# include <iostream>
using namespace std;

class Employee
{
public:
int GetAge() const;
void SetAge(int age);
int GetYearsOfService()const;

void SetYearsOfService(int years);
int GetSalary()const;
void SetSalary(int salary);

private:
int Age;
int YearsOfService;
int Salary;
};

int Employee::GetAge()
{
return Age;
}

void Employee::SetAge(int age)
{
Age = age;
}

int Employee::GetYearsOfService()
{
return YearsOfService;
}

void Employee::SetYearsOfService(int years)
{
YearsOfService = years;
}

int Employee::GetSalary()
{
return Salary;
}

void Employee::SetAge(int salary)
{
Salary = salary;
}

int main()
{
Employee John;
Employee Sally;
John.SetAge(30);
John.SetYearsOfService(5);
John.SetSalary(50000);

Sally.SetAge(32);
Sally.SetYearsOfService(8);
Sally.SetSalary(40000);

count << "At a company, John and Sally work.\n";
count << "John is " << John.GetAge() << " years old and has been working for";
count << John.GetYearsOfService() << " years and is paid" << John.GetSalary() << "annually.\n\n";
count << "Sally is" << Sally.GetAge() << " years old and has been working for";
count << Sally.GetYearsOfService() << " years and is paid" << John.GetSalary() << "annually.\n\n";
return 0;
}


I really need help with this. I copied the exact code in the back of my textbook that provides the answer for this code, and I still got errors. So I changed it a bit and I'm still getting errors. I would greatly appreciate any help!
 
Physics news on Phys.org
I think you'll have to work this out for yourself. What errors are you getting? What does it say?
 
I think it said that the int Employee:GetAge() from:

int Employee::GetAge()
{
return Age;
}


was overloaded or some kind of function error. The same errors showed up for the yearsofservice and salary.
 
There ARE two definitions of SetAge and one actually sets the salary! I'd pay attention to the compiler errors and fix them one by one. You are also having problems because you are declaring functions as 'const' and then not implementing them as const.
 
I fixed the errors and it worked! thanks a lot for the help!