Hi,
Here is some advice to help you out in the future, am putting some ideas & concepts out there, some of it may be a shade advanced right now, but there you go :+)
Just wondering what is happening here?
279 280 281 282 283 284 285 286 287 288 289
|
Employee1.getFirstName();
Employee1.getLastName();
Employee1.getGender();
Employee1.getDependents();
Employee1.getAnnualSalary();
Employee1.setFirstName(firstName);
Employee1.setLastName(lastName);
Employee1.setGender(gender);
Employee1.setDependents(dependents);
Employee1.setAnnualSalary(annualSalary);
|
The get functions are accessing un-initialised member data as far as I can see. Better to set them first?
A better way of doing things is to use an initialiser list in a constructor:
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
|
// spread these out parameters 1 per line - easier to read
// and can be commented - do this for any function that has multiple parameters
Employee::Employee (
// variables here for any base class if necessay
const std::string& first , // Pass by reference for string
const std::string& last , //
const char gen , // Pass by value for basic types
const int dep , // could be unsigned short int?
const double salary ,
const Benefit& ben , // Pass by reference for user defined types
// numEmployees // not included because its static - do you even need it?
// can get size of container from it 's member function
)
: // colon introduces initialiser list
// member variables are initialised before
// construction, not assigned to again after construction
// make sure to do each member variable, don't miss any
// call any base class constructors if needed (in order) here to avoid object slicing
firstName (first) , // can put comments here too
lastName (last),
gender (gen) ,
dependents (dep) ,
annualSalary (salary) ,
benefit (ben) ,
// numEmployees // not included because its static
{
// numEmployees++; // probably don't need it
}
|
Doing things this way means you probably don't need all those set functions.
Set functions are only required when the object is changed / updated after it has been created. Even then it is often possible to think about what has to
happen to an object, then write a function that updates several related or all of the member variables at once.
For updating, one might have a member
UpdateAddress
function as an general example. Remember member functions have direct access to member variables - so one would do that rather than call lots of
set
functions.
Another example for output one could write a member
PrintDetails
function, rather than calling all the
get
functions. Or overload the
ostream
<<
operator.
So as it stands at the moment, you don't seem to need
any get
or
set
functions that I can see.
Instead of making an
Employee1
object, consider using a
std::vector
(if allowed to) or at least an array of them. Use the
size
function which belongs to
std::vector
to reveal how many employees you have. Or count them as you put them into the array.
With
get
functions (if you are going to have them), they should be marked
const
, because they don't change the state of the object - that is the values of all the member variables remain the same. Parameters to functions should also be marked
const
where possible, one usually doesn't want these to be altered in the body of the function (example below), unless passing by reference in order to modify a variable in the calling scope. Also, they should return
const
values or a
const
reference, one generally doesn't want anyone to change the returned value afterwards:
1 2 3 4 5
|
const string& Employee::getFirstName() const
{
return firstName;
}
|
1 2 3 4 5
|
const double Employee::getAnnualSalary() const
{
return annualSalary;
}
|
This is all called
const
correctness - on should use
const
wherever possible. Note that a const function is separate to a non const one, even though their signatures are otherwise the same:
1 2 3 4 5 6 7 8 9
|
const double MyClass::MyDoubleFunc(const double MyDouble1 , const double MyDouble2) const {
//code that doesn't change the state of the class
return MyDouble ;
}
const double MyClass::MyDoubleFunc(const double MyDouble1 , const double MyDouble2) {
// code that might change the state of the class
return MyDouble;
}
|
As well as that, returning a reference to a local variable doesn't work - it gets destroyed as soon as it goes out of scope:
1 2 3 4 5
|
std::string& MyFunction () {
// local variable
std::string MyString("Me");
return MyString; // error returning local reference
}
|
So there you go - I hope you find all this useful <8+D
regards & cheers
Edit:
Hell, I forgot to say - Well done on your original code :+)