Class/Overloaded funtion help
-
Im reading "teach yourself C++ in 21 days", well Ive been stuck on day 6 for about a week now. Can anyone tell me whats wrong with this source? I keep getting overloaded function errors but I really want to understand this before I move on to day 7. thanks /* Chapter 6 end of lesson program Teach yourself C++ in 21 days */ // simple program to test my knowlege of classes... // make a class that assignes and reads values using public accessors #include class Employee { public: int GetAge() const; void SetAge(int age); int GetYearsOfService() const; void SetYearsOfService(int yearsOfService) const; //Public accessors so that the variables can remain private int GetSalary() const; void SetSalary(int salary); private: int age; int yearsOfService; //Private variables int salary; }; int Employee::GetAge() { return age; } void Employee::SetAge(int age2) { age = age2; } int Employee::GetYearsOfService() { return yearsOfService; } void Employee::SetYearsOfService(int yearsOfService2) { yearsOfService = yearsOfService2 } int Employee::GetSalary() { return salary; } void Employee::SetSalary(int salary2) { salary = salary2; } void main() { int age2, yearsOfService2, salary2; Employee Matt; //define 2 objects of class employee Employee Max; Matt.SetAge(21); Max.SetAge(35); Matt.SetYearsOfService(2); //assign values to the 2 objects Max.SetYearsOfService(6); Matt.SetSalary(20000); Max.SetSalary(34600); cout<<"Matts age is : "; cout<
-
Im reading "teach yourself C++ in 21 days", well Ive been stuck on day 6 for about a week now. Can anyone tell me whats wrong with this source? I keep getting overloaded function errors but I really want to understand this before I move on to day 7. thanks /* Chapter 6 end of lesson program Teach yourself C++ in 21 days */ // simple program to test my knowlege of classes... // make a class that assignes and reads values using public accessors #include class Employee { public: int GetAge() const; void SetAge(int age); int GetYearsOfService() const; void SetYearsOfService(int yearsOfService) const; //Public accessors so that the variables can remain private int GetSalary() const; void SetSalary(int salary); private: int age; int yearsOfService; //Private variables int salary; }; int Employee::GetAge() { return age; } void Employee::SetAge(int age2) { age = age2; } int Employee::GetYearsOfService() { return yearsOfService; } void Employee::SetYearsOfService(int yearsOfService2) { yearsOfService = yearsOfService2 } int Employee::GetSalary() { return salary; } void Employee::SetSalary(int salary2) { salary = salary2; } void main() { int age2, yearsOfService2, salary2; Employee Matt; //define 2 objects of class employee Employee Max; Matt.SetAge(21); Max.SetAge(35); Matt.SetYearsOfService(2); //assign values to the 2 objects Max.SetYearsOfService(6); Matt.SetSalary(20000); Max.SetSalary(34600); cout<<"Matts age is : "; cout<
- Overloaded function errors due to missing const qualifier in function definitions When you declare a member function constant, the const qualifier is also required in the function definition (and declaration). For example, class Employee { ... int GetAge() const; ... }; int Employee::GetAge() const{ return age; } 2) Don't declare SetYearsOfService() as a constant member function (leave off the const at the end) 3) Missing ; in SetYearsOfService() void Employee::SetYearsOfService(int YearsOfService2) { yearsOfService=yearsOfService2; }
-
- Overloaded function errors due to missing const qualifier in function definitions When you declare a member function constant, the const qualifier is also required in the function definition (and declaration). For example, class Employee { ... int GetAge() const; ... }; int Employee::GetAge() const{ return age; } 2) Don't declare SetYearsOfService() as a constant member function (leave off the const at the end) 3) Missing ; in SetYearsOfService() void Employee::SetYearsOfService(int YearsOfService2) { yearsOfService=yearsOfService2; }
thanks, I feel the headache going away already :)