OK...I am going to type out the entire assignment as specified by the Prof (without his typos) :-) |
Awww Hell, sorry I made you do that. <(:+(
Teacher wrote:
You will write the set member function (like setFirstName, setLastName, etc.) to set the value of the data members as provided by the user at runtime and the get member function (like getFirstName, getLastName, etc.) to retirieve these values for displaying. |
I am sure that I am not alone in saying that it is a shame that teachers
perpetuate this misconception.
Although they didn't say the get / set function [b]had to be public[/b].It is very easy to see how almost anyone would interpret the specification wrongly.
And the reason for not having public get / set functions is solid: If the get /set functions are public (they have to be)
then you might as well make all the member variables public !!!!!!!
It is much better to do what you are doing now, although see my recommendation at the end for an even better idea. I would point that out to the teacher, diplomatically. I would be irate, if I lost marks for doing something correctly.
To flesh out a similar example, consider a customer information scenario. Have functions that set info split into logical groups - setNames (all of them), setAddress (3 address lines ,suburb, city, state, postcode), setEmail (all of them, 3 say) and so on. When you have a new customer, have a function that calls all the other functions. Sometimes it is worth it for a function to set one member variable, for example a phone number - people often might change their home number or mobile, not so good to have a function that forces you to re input all (5 or more say) of them.
Actually, when there are lots of variable to initialise, it is a good idea for a constructor to call an Initialise function, which would in turn call the other functions mentioned. This way you are making use of the functions for a new object, and they can be used to change an existing object.
Someone might argue that, if all these functions are public, then there is still no difference between that and making all the member variables public. That is true, the only real thing we have done is avoiding having lots of function calls, and perhaps made it a bit more logical.
If you were really worried about security, you could make all the functions private (or more likely protected) then have an authorisation function which involved a PIN number, which would then give access to a menu, which would call the functions. This provides some security for the actual users.
Even the menu by itself would be OK, because now you don't expose functions that can change data. This is a form of security for the application - to guard against other programmers who might use your object in an illegitimate way. I am talking about other coders, because in the real IT world, you write code that will be reused in some way by others.
So, now I am saying you should have one public ShowMenu function, which calls the other functions which should be private. The ShowMenu function should use a switch statement to make it elegant and scalable. Explain all this with comments in the code so your teacher can see why you have done it.
I am sorry that I have now specified something different again - but the upside might be you really understand well, because the ideas have evolved conceptually, from bad, to medium, to good.
Any way I hope you get a really good mark for the assignment.