Comp Sci Creating and Printing Employee Data - C++ Homework Solution

  • Thread starter Thread starter proton
  • Start date Start date
  • Tags Tags
    C++ Homework
AI Thread Summary
The discussion revolves around a C++ homework problem requiring the creation of an Employee class to manage employee data, including age, years of service, and salary. The user encountered errors while trying to implement the code, specifically related to function overloading and incorrect function definitions. A significant issue was identified with the SetAge method, which was mistakenly defined twice, leading to confusion in the program. Additionally, there were problems with the const qualifier in function declarations and definitions. After addressing these issues, the user successfully resolved the errors with assistance from others in the forum.
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);

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!
 
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!
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
2
Views
2K
Replies
5
Views
2K
Replies
4
Views
1K
Replies
8
Views
2K
Replies
1
Views
10K
Replies
14
Views
4K
Back
Top