Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ homework

  1. Mar 1, 2007 #1
    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
    int GetAge() const;
    void SetAge(int age);
    int GetYearsOfService()const;

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

    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;


    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!
  2. jcsd
  3. Mar 2, 2007 #2


    User Avatar
    Homework Helper

    I think you'll have to work this out for yourself. What errors are you getting? What does it say?
  4. Mar 2, 2007 #3
    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.
  5. Mar 2, 2007 #4


    User Avatar
    Science Advisor
    Homework Helper

    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.
  6. Mar 3, 2007 #5
    I fixed the errors and it worked! thanks alot for the help!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook