I am using Ubuntu; my compiler is Code::Blocks.
The instructions at the bottom are the exercises for one of the lessons in http://newdata.box.sk/bx/c/htm/ch06.htm#Heading2, from the book "Teach Yourself C++ in 21 Days". I have no idea why this won't compile. It sends an error message at 'int setage'.
#include <iostream>
usingnamespace std;
class employee
{
public:
int setAge(int itsAge);
int setyearsOfService(int itsyearsOfService);
int setsalary(int itssalary);
void getAge() const;
void getyearsOfService() const;
void getsalary() const;
private:
int age;
int yearsOfService;
int salary;
}
/*Functions declared between here and the next line of comments.*/
void employee::setAge(itsAge) {itsAge = age;}
void employee::setyearsOfService(itsyearsOfService){itsyearsOfService = yearsOfService;}
void employee::setsalary(itssalary)
{
itssalary = salary;
}
int employee::getAge()
{
return age;
}
int employee::getyearsOfService()
{
return yearsOfService;
}
int employee::getsalary()
{
return salary;
}
/*Functions declared between here and the previous line of comments.*/
int main()
{
employee Carl;
Carl.setage(35);
Carl.setyearsOfService(2);
Carl.setsalary(30000);
Carl.getAge();
Carl.getyearsOfService();
Carl.getsalary();
employee Cindy;
Cindy.setage(33);
Cindy.setyearsOfService(1);
Cindy.setsalary(29000);
Cindy.getAge();
Cindy.getyearsOfService();
Cindy.getsalary();
return 0;
}
/*
1. Write the code that declares a class called Employee with these data members: age, yearsOfService, and Salary.
2. Rewrite the Employee class to make the data members private, and provide public accessor methods to get and set each of the data members.
3. Write a program with the Employee class that makes two Employees; sets their age, YearsOfService, and Salary; and prints their values.
4. Continuing from Exercise 3, provide a method of Employee that reports how many thousands of dollars the employee earns, rounded to the nearest 1,000.
5. Change the Employee class so that you can initialize age, YearsOfService, and Salary when you create the employee.
*/
Of what type is itsAge? Simply declaring a new symbol without a type-specifier is illegal. You didn't happen to come from a scripting background, did you? Just asking :)
Do not use that book. Aside from the fact that it isn't free and is likely published illegally on that site, it's one of the worst C++ books that exist (if it even deserves the description "C++ book" - what it teaches is not valid C++).
@Framework/Wazzak
No I didn't. This is my first programming experience, other than a failed attempt at Visual Basic a few years ago. I think I can blame that on Microsoft's awful tutorials though.
@Athar
Alrighty; I'll give that a try.
I updated the code and added integers to the function declaration within 'employee'. Now the error is where I try to define all the accessor functions.