Hi
hagfish88,
This maybe a surprise for you, but I don't think you need get & set functions at all !
We had a huge discussion about this here:
I'll try to do an executive summary for you:
-. Naive (straight assignment) set functions are really bad. For example
hagfish.setAcctBalance(-1000000);
Use constructors, with initialiser lists instead.
-. If each class member has a public get / set function, then all the variables might as well be public !!
-. Functions that Update a value after it has been constructed, should have validation code. For example a Withdrawal function might check whether the user is authorised, and if there is sufficient balance say.
-. Get functions are no where near as bad, but are only really needed when access to a variable is needed from outside the class.
-. Don't use get functions when a member function would be better. For example you have this in main():
60 61 62 63
|
cout << "First Name: " << paperone.getFirstName();
cout << "Last Name: " << paperone.getLastName();
cout << "Subject: " << paperone.getSubject();
cout << "Grade: " << paperone.getGrade();
|
Instead you could have a member function like this :
1 2 3 4 5 6 7
|
void TermPaper::PrintDetails() {
std::cout << "First Name: " << m_FirstName << "\n";
std::cout << "Last Name: " << m_LastName << "\n";
std::cout << "Subject: " << m_Subject << "\n";
std::cout << "Grade: " << m_Grade << "\n";
}
|
This idea is part of the concept of encapsulation : Generally, all the functionality and data for an object should be in the object's class.
The problem is that book authors teach the idea of having private variables (which is right), so then one needs accessor & mutator functions (get / set), but then they don't explain how to do that properly.
If you change you code to implement these suggestions, then you will probably have much less & better code, and might even get better marks, if this is an assignment.
Hope all goes well 8+)