C++ - Basic Classes

I'm reading a book called SAMS Teach Yourself C++ For Linux in 21 Days, and I just read "Day 6 - Basic Classes". I feel that I have grasped the concept (a little), and I wanted to ensure my knowledge by writing my own example. The example includes two files, "employee.hpp" and "employee.cxx". I believe I have written it correctly, but I'm having problems. Here's the source, and the output.

employee.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Sandbox test
#include <iostream>
using namespace std;
class Employee{
public:
	int GetAge()const {return itsAge;}
	int GetYearsOfService()const {return itsYearsOfService;}
	int GetSalary()const {return itsSalary;}
	void SetAge(int age) {itsAge = age;}
	void SetYearsOfService(int yearsOfService) {itsYearsOfService = yearsOfService;}
	void SetSalary(int salary) {itsSalary = salary;}
private:
	int itsAge;
	int itsYearsOfService;
	int itsSalary;
}


employee.cxx
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
#include "employee.hpp"
int main(){
employee::employee(int initialAge, int initialYearsOfService, int initialSalary){
	itsAge = initialAge;
	itsYearsOfService = initialYearsOfService;
	itsSalary = initialSalary;
}
employee::~employee{}
	int input;
	Employee John(35,10,60000);
	cout << "John is " << John.GetAge() << " years old.\n";
	cout << "John has " << John.GetYearsOfService() << " years of service.\n";
	cout << "John earns $" << John.GetSalary() << " a year.\n";
	cout << "Would you like to change these values (enter y or n)?\n";
	cin >> input;
	if(input == "y" || input == "Y"){
		cout << "Enter John's age: ";
		cin >> input;
		John.SetAge(input);
		cout << "\nEnter John's years of service: ";
		cin >> input;
		John.SetYearsOfService(input);
		cout << "\nEnter John's annual salary: ";
		cin >> input;
		John.SetSalary(input);
	} else if(input == "n" || input == "N"){
		cout << "Values have remained the same.\n";
	} else {
		cout << "Incorrect input.\n";
	}
	return 0;
} 


anonymous@anonymous:~/sandbox$ g++ employee.cxx -o employee.exe
employee.hpp:4: error: new types may not be defined in a return type
employee.hpp:4: note: (perhaps a semicolon is missing after the definition of ‘Employee’)
employee.cxx:2: error: two or more data types in declaration of ‘main’
anonymous@anonymous:~/sandbox$


It's not especially important, but I just want to make sure I'm doing things right. Can anyone see what's wrong with my code? Thanks!
Here are clear errors:

1) You class needs to have a semicolon after the closing brace:

1
2
3
4
class employee
{
  // stuff here
};  // don't forget this semicolon 


2) You can't define bodies for your ctor (employee::employee) or dtor (employee::~employee) if you never put a ctor or dtor in your class.

1
2
3
4
5
6
class employee
{
public:
           // if you want to have ctor/dtor bodies....
  employee();  // .. you need to have a ctor here
  ~employee();  //  and a dtor 


3) You forgot the parenthesis on line 8 of your .cxx file. Should be: employee::~employee() { }. But you don't need a dtor for this class if it's empty. You can leave that whole thing out.

4) Don't define the function bodies inside other functions. IE: your ctor/dtor bodies should not be inside main like that.


Another thing you might change:

1) Get/Set for every member var is dumb. Some books teach this because there's supposedly some "rule" that member vars shouldn't be public. But it's ridiculous to think that. There's no real need for Get/Sets here.

I've made these changes that you suggested (except for the Get/Set change), but I'm still having problems. In fact, I've opened myself up to a whole new list of errors. Here's the new employee.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Sandbox test, quiz question 6.1
#include <iostream>
using namespace std;
class Employee{
public:
	employee();
	~employee();
	int GetAge()const {return itsAge;}
	int GetYearsOfService()const {return itsYearsOfService;}
	int GetSalary()const {return itsSalary;}
	void SetAge(int age) {itsAge = age;}
	void SetYearsOfService(int yearsOfService) {itsYearsOfService = yearsOfService;}
	void SetSalary(int salary) {itsSalary = salary;}
private:
	int itsAge;
	int itsYearsOfService;
	int itsSalary;
};


And employee.cxx
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
#include "employee.hpp"
employee::employee(int initialAge, int initialYearsOfService, int initialSalary){
	itsAge = initialAge;
	itsYearsOfService = initialYearsOfService;
	itsSalary = initialSalary;
}
employee::~employee() {}
int main(){

	int input;
	Employee John(35,10,60000);
	cout << "John is " << John.GetAge() << " years old.\n";
	cout << "John has " << John.GetYearsOfService() << " years of service.\n";
	cout << "John earns $" << John.GetSalary() << " a year.\n";
	cout << "Would you like to change these values (enter y or n)?\n";
	cin >> input;
	if(input == "y" || input == "Y"){
		cout << "Enter John's age: ";
		cin >> input;
		John.SetAge(input);
		cout << "\nEnter John's years of service: ";
		cin >> input;
		John.SetYearsOfService(input);
		cout << "\nEnter John's annual salary: ";
		cin >> input;
		John.SetSalary(input);
	} else if(input == "n" || input == "N"){
		cout << "Values have remained the same.\n";
	} else {
		cout << "Incorrect input.\n";
	}
	return 0;
}

I'm probably doing something else wrong (or misunderstanding). But I've quite a few more compiler errors, it's like a monster.

anonymous@anonymous:~/sandbox$ g++ employee.cxx -o employee.exe
In file included from employee.cxx:1:
employee.hpp:6: error: ISO C++ forbids declaration of ‘employee’ with no type
employee.hpp:7: error: expected class-name before ‘(’ token
employee.cxx:2: error: ‘employee’ has not been declared
employee.cxx:2: error: ISO C++ forbids declaration of ‘employee’ with no type
employee.cxx: In function ‘int employee(int, int, int)’:
employee.cxx:3: error: ‘itsAge’ was not declared in this scope
employee.cxx:4: error: ‘itsYearsOfService’ was not declared in this scope
employee.cxx:5: error: ‘itsSalary’ was not declared in this scope
employee.cxx: At global scope:
employee.cxx:7: error: expected constructor, destructor, or type conversion before ‘::’ token
employee.cxx: In function ‘int main()’:
employee.cxx:11: error: no matching function for call to ‘Employee::Employee(int, int, int)’
employee.hpp:4: note: candidates are: Employee::Employee()
employee.hpp:4: note:                 Employee::Employee(const Employee&)
employee.cxx:17: error: ISO C++ forbids comparison between pointer and integer
employee.cxx:17: error: ISO C++ forbids comparison between pointer and integer
employee.cxx:27: error: ISO C++ forbids comparison between pointer and integer
employee.cxx:27: error: ISO C++ forbids comparison between pointer and integer
anonymous@anonymous:~/sandbox$ 


So I've probably done something wrong in trying to fix my errors. Any suggestions?

1) You're inconsistent with your class name. Sometimes you have Employee and sometimes you have employee. Either capitalize it or don't -- remember that C++ is case sensitive, so you need to pick one and use it everywhere.

2) Your prototyped ctor doesn't match your ctor's body:

employee(); // in your header it takes no parameters
1
2
// in your .cxx it takes 3 parameters:
employee::employee(int initialAge, int initialYearsOfService, int initialSalary)


Change the prototype to correctly reflect the number of parameters.


3) Think about this:

1
2
3
4
int input;  // notice 'input' is an integer
//...
cin >> input;
if(input == "y" || input == "Y"){  // do you see what's wrong here? 
Ok, I've taken all this into account. Now I'm down to four errors (on two lines). The lines are:
 
if(input == "y" || input == "Y"){


and
 
} else if(input == "n" || input == "N"){


From the terminal:

anonymous@anonymous:~/sandbox$ g++ employee.cxx -o employee.exe
employee.cxx: In function ‘int main()’:
employee.cxx:17: error: ISO C++ forbids comparison between pointer and integer
employee.cxx:17: error: ISO C++ forbids comparison between pointer and integer
employee.cxx:28: error: ISO C++ forbids comparison between pointer and integer
employee.cxx:28: error: ISO C++ forbids comparison between pointer and integer


So, I realise you're trying to tell me something about this comparison, so I tried declaring char input;,
but this did nothing to fix the error. I must be missing something here (I'm not the best at this). Any ideas? Thanks!

single quotes ('y') is char.
double quotes ("y") is a string.

Since you're comparing the input to a string, you should use a string. Either that, or use a char and compare to a char. But comparing an integer to a string (or a char to a string) won't work because they're different types.

But that's not all. input is ALSO used as an integer in your code (for the age, salary, etc). So you'll probably need to have two variables for input. One which is a string/char, and another which is an integer.
Thank you for your help. Actually, that last note about input storing an integer, I already declared a separate variable. I added a few lines of code to print the results, to ensure it worked. In case you're interested, here's the final code:

employee.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Sandbox test
#include <iostream>
using namespace std;
class employee{
public:
	employee(int, int, int);
	~employee();
	int GetAge()const {return itsAge;}
	int GetYearsOfService()const {return itsYearsOfService;}
	int GetSalary()const {return itsSalary;}
	void SetAge(int age) {itsAge = age;}
	void SetYearsOfService(int yearsOfService) {itsYearsOfService = yearsOfService;}
	void SetSalary(int salary) {itsSalary = salary;}
private:
	int itsAge;
	int itsYearsOfService;
	int itsSalary;
};


employee.cxx
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
//Sandbox Test
#include "employee.hpp"
employee::employee(int initialAge, int initialYearsOfService, int initialSalary){
	itsAge = initialAge;
	itsYearsOfService = initialYearsOfService;
	itsSalary = initialSalary;
}
employee::~employee() {}
int main(){

	string input;
	employee john(35, 10, 60000);
	cout << "John is " << john.GetAge() << " years old.\n";
	cout << "John has " << john.GetYearsOfService() << " years of service.\n";
	cout << "John earns $" << john.GetSalary() << " a year.\n";
	cout << "Would you like to change these values (enter y or n)?\n";
	cin >> input;
	if(input == "y" || input == "Y"){
		int input2;
		cout << "Enter John's age: ";
		cin >> input2;
		john.SetAge(input2);
		cout << "\nEnter John's years of service: ";
		cin >> input2;
		john.SetYearsOfService(input2);
		cout << "\nEnter John's annual salary: ";
		cin >> input2;
		john.SetSalary(input2);
		cout << "Values changed.\n";
		cout << "John is " << john.GetAge() << " years old.\n";
		cout << "John has " << john.GetYearsOfService() << " years of service.\n";
		cout << "John earns $" << john.GetSalary() << " a year.\n";
	} else if(input == "n" || input == "N"){
		cout << "Values have remained the same.\n";
	} else {
		cout << "Incorrect input.\n";
	}
	return 0;
}


And here's the output from the terminal:

anonymous@anonymous:~/sandbox$ g++ employee.cxx -o employee.exe
anonymous@anonymous:~/sandbox$ ./employee.exe
John is 35 years old.
John has 10 years of service.
John earns $60000 a year.
Would you like to change these values (enter y or n)?
y
Enter John's age: 40

Enter John's years of service: 15

Enter John's annual salary: 100000
Values changed.
John is 40 years old.
John has 15 years of service.
John earns $100000 a year.


Again, thank you for your help. I think I'll write a second one of these, and try to work on my typing errors (and debug it for myself). Thank you for your help. Good day!

Topic archived. No new replies allowed.