• Support PF! Buy your school textbooks, materials and every day products Here!

C++ homework

  • Comp Sci
  • Thread starter proton
  • Start date
  • #1
350
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);

cout << "At a company, John and Sally work.\n";
cout << "John is " << John.GetAge() << " years old and has been working for";
cout << John.GetYearsOfService() << " years and is paid" << John.GetSalary() << "annually.\n\n";
cout << "Sally is" << Sally.GetAge() << " years old and has been working for";
cout << 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!
 

Answers and Replies

  • #2
verty
Homework Helper
2,164
198
I think you'll have to work this out for yourself. What errors are you getting? What does it say?
 
  • #3
350
0
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.
 
  • #4
Dick
Science Advisor
Homework Helper
26,258
618
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.
 
  • #5
350
0
I fixed the errors and it worked! thanks alot for the help!
 

Related Threads on C++ homework

  • Last Post
Replies
2
Views
1K
Replies
4
Views
6K
  • Last Post
Replies
0
Views
1K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
4
Views
3K
Replies
7
Views
1K
  • Last Post
Replies
2
Views
9K
Replies
7
Views
902
  • Last Post
Replies
0
Views
3K
  • Last Post
Replies
5
Views
1K
Top