Creating and Printing Employee Data - C++ Homework Solution

  • Context: Comp Sci 
  • Thread starter Thread starter proton
  • Start date Start date
  • Tags Tags
    C++ Homework
Click For Summary

Discussion Overview

The discussion revolves around a C++ homework problem involving the creation and management of an Employee class. Participants explore issues related to code errors, function definitions, and the implementation of class methods.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster describes their task of creating an Employee class and expresses confusion over errors encountered in their code.
  • One participant suggests that the original poster should identify the specific errors being reported by the compiler.
  • The original poster mentions an error related to the function definition of GetAge, indicating potential overloading issues.
  • Another participant points out that there are two definitions of the SetAge function, which is causing confusion, and notes the importance of matching const declarations with implementations.
  • The original poster later reports that they fixed the errors and the code is now functioning correctly.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the specific nature of the errors initially, but there is agreement on the need to address function definitions and compiler messages. The discussion appears to resolve with the original poster successfully fixing their code.

Contextual Notes

Participants reference specific compiler errors and function definitions, but the exact nature of the errors and the original code's issues remain unspecified. There is a mention of const correctness that may imply additional considerations in the code implementation.

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!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K