@
anirudh sn
I think it would be worth your while to read the discussion in the link I provided.
anirudh sn wrote: |
---|
u cant invoke a constructor just to set/reset the values, constructors vs setters is not a valid comparison!!! |
But a constructor with an initialiser list can set values
initially and might mean there is no need for set functions at all. I did mention update functions, and I did mention the naivety of having straight assignment in a set function. And I also said get functions are rather benign.
anirudh sn wrote: |
---|
Ive seen getters/setters in many real time applications, for some critical cases getters/setters are made protected/not given, it is nonsense to say that getters/setters are not used. |
I also said:
TheIdeasMan wrote: |
---|
I am not saying that they shouldn't be used at all, just that this is a classic situation where they are used very poorly. |
And I was talking about
public set functions.
anirudh sn wrote: |
---|
Tell me how do u think balance info is fetched from a bank acc?? its a getter method, getter and setter methods are evrywhere, if the variable is very critical then they are not provided or made private!!! |
Would you care to tell us what your bank balance is right now? Or even better should there be a way I can look it up on the net without even asking you, then transfer your money to my account? Again I am talking about public functions. Which is what you proposed to the OP. You also proposed friend functions, but again this isn't appropriate for this situation.
What I mean is that there may be a get function used internally, but it certainly should not be public. Think of an ATM machine - there would be layers between the ATM and the actual account. After identification and authorisation, the ATM class makes a transaction request to the Transaction class, which queries the account class and decides whether the transaction can be approved. There is no
ATM.Withdraw("anirudh sn", 100000.00)
function that works directly on your account. The account class is unknown to the ATM class.
So, for the OP's program. I see it like this:
There is just an Account class, plus some container for putting them in.
We need to set values when the account is first created - name & other personal details, an initial balance of 0.00. This can be done with a constructor with an initialiser list, provided that an STL container is used to store them, so we can call the constructor with arguments and use it's initialiser list. So no set functions needed here.
If an array is used (the default constructor is called, so values are already initialised), then one could have a small number (or even just one) of set functions that set several member variables inside the function - SetName, SetAddress, SetDOB etc say. We do not have a set function for each member variable, and they have checking, validation, identification & authorisation checks. They are not just straight assignment.
The two Update (mutator) functions that are needed as a minimum are Deposit & Withdraw. Others such as change address might also be required. These are public functions that take arguments, do checking & validation, and are not straight assignment.
Printing out values is also a member function, it has direct access to the member variables. It is poor form to use get functions from main() for this purpose.
Finally, I think the topic of get /set functions falls in the list of things that are often done routinely & badly by beginners (they frequently point to bad design), but there are some valid situations for their use. Beginners often do this because they see it as being easier, Some of these might be:
- Infinite loops
- get / set functions
- goto
- friend functions / classes
- dynamic casting
- RTTI