Jun 27, 2010 at 4:48am UTC
Hello all! I'm having an aneurysm right now, and I'm hoping y'all can help. I'm working through internet tuts and Sam's Teach Yourself right now to help me learn C++. I'm trying to do an exercise for Day 6: Basic Classes in the Sam's book, and I keep getting compile errors saying that my class objects do not have an identifier, and my accessor methods are lacking a class, and I can't figure out what's wrong. I have 3 files, Employee.h, Employee.cpp, and MainProgram.cpp, all in the same directory. I have included the full code for each below.
// Employee.h///////////////////////////////////////////////////////////////////
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
class Employee
{
public :
Employee(int initialAge, int initialYearsofService, double initialSalary);
~Employee();
int GetAge();
void SetAge(int Age);
int GetYearsofService();
void SetYearsofService(int YearsofService);
double GetSalary();
void SetSalary(double Salary);
double GetEarnings();
private :
int age;
int yearsofservice;
double salary;
};
// Employee.cpp ////////////////////////////////////////////////////////////////
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#include "Employee.h"
Employee::Employee(int initialAge, int initialYearsofService, double initialSalary)
{
age = initialAge;
yearsofservice = initialYearsofService;
salary = initialSalary;
}
Employee::~Employee()
{
}
int Employee::GetAge()
{
return (age);
}
void Employee::SetAge(int Age)
{
age = Age;
}
int Employee::GetYearsofService()
{
return (yearsofservice);
}
void Employee::SetYearsofService(int YearsofService)
{
yearsofservice = YearsofService;
}
double Employee::GetSalary()
{
return (salary);
}
// MainProgram.cpp /////////////////////////////////////////////////////////////
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#include <iostream>
void EndProg();
int main()
{
Employee emp1 (5, 5, 5.0);
Employee emp2 (5, 5, 5.0);
Employee emp3 (5, 5, 5.0);
int emp1age, emp2age;
int emp1yearsofservice, emp2yearsofservice;
double emp1salary, emp2salary;
std::cout << "Information for Employee #1..." << std::endl;
std::cout << "Please enter the employee's age: " ;
std::cin >> emp1age;
emp1.SetAge(emp1age);
std::cout << "Please enter the employee's years of service: " ;
std::cin >> emp1yearsofservice;
emp1.SetYearsofService(emp1yearsofservice);
std::cout << "Please enter the employee's monthly salary: " ;
std::cin >> emp1salary;
emp1.SetSalary(emp1salary);
std::cout << std::endl << std::endl;
std::cout << "Information for Employee #2..." << std::endl;
std::cout << "Please enter the employee's age: " ;
std::cin >> emp2age;
emp2.SetAge(emp2age);
std::cout << "Please enter the employee's years of service: " ;
std::cin >> emp2yearsofservice;
emp2.SetYearsofService(emp2yearsofservice);
std::cout << "Please enter the employee's monthly salary: " ;
std::cin >> emp2salary;
emp2.SetSalary(emp2salary);
std::cout << std::endl << std::endl;
std::cout << "Printing Employee #1's information..." << std::endl;
std::cout << "Employee #1's age: " << emp1.GetAge() << std::endl;
std::cout << "Employee #1's years of service: " << emp1.GetYearsofService() << std::endl;
std::cout << "Employee #1's monthly salary: " << emp1.GetSalary() << std::endl;
std::cout << "Employee #1's yearly salary: " << emp1.GetEarnings() << std::endl << std::endl;
std::cout << "Printing Employee #2's information..." << std::endl;
std::cout << "Employee #2's age: " << emp2.GetAge() << std::endl;
std::cout << "Employee #2's years of service: " << emp2.GetYearsofService() << std::endl;
std::cout << "Employee #2's monthly salary: " << emp2.GetSalary() << std::endl;
std::cout << "Employee #2's yearly salary: " << emp2.GetEarnings() << std::endl << std::endl;
std::cout << "Printing Employee #3's information..." << std::endl;
std::cout << "Employee #3's age: " << emp3.GetAge() << std::endl;
std::cout << "Employee #3's years of service: " << emp3.GetYearsofService() << std::endl;
std::cout << "Employee #3's monthly salary: " << emp3.GetSalary() << std::endl;
std::cout << "Employee #3's yearly salary: " << emp3.GetEarnings() << std::endl << std::endl;
EndProg();
return (0);
}
void EndProg()
{
int condition;
std::cout << "Please enter '0' in order to exit the program: " ;
std::cin >> condition;
if (condition != 0)
EndProg();
}
Last edited on Jun 27, 2010 at 4:56am UTC
Jun 27, 2010 at 4:53am UTC
Bah... ty, I feel like a tool. Guess I just needed another set of eyes. Thanks again.
Edit: Also, I'm new here, so sorry about the tags. I will totally use the tags next time.
Last edited on Jun 27, 2010 at 4:55am UTC
Jun 27, 2010 at 5:14am UTC
Happens to me too, don't worry about it too much ;)