Thomas1965 wrote: |
---|
In this case a setter doesn't make much sense. |
Not sure what you meant there, it trivial but often still necessary, have to have some way of accessing a private variable.
Thomas1965 wrote: |
---|
Another common scenario is to fire a change event if you implement the observer pattern |
I think there are a number of concepts the OP has to get down before we start talking about Design Patterns :+)
Hi Chay,
You are doing the right thing by having a set function. But the question from you was why do we do this?
It is good to have member variables private, that is so constructors can initialize them and only member functions can change their value.
The advantage of having a function to set a member variable's value is that it can perform checks, for example ask for a PIN number for a bank account. Basically we want to see if the arguments to the set function make sense
Another advantage is if the underlying representation changes, as in an angle being stored as degrees , is changed to radian, then the set function arguments can remain the same, so it is the same for whoever is calling the function. In other words it doesn't break the caller's code. You see, what happens is that people/ organizations write code (a library) which they give to their clients, who then use the classes / functions from the library in their own code which could be millions of lines long. It would be bad if the client had to change all their code just because someone wanted to make a minor change to the library. So we always strive to write code in such a way that we can change functions internally without changing the way the function is called.
The alternative to not having
private
variables is to have the variables
public
, which is obviously bad. There are some situations where we can have a fully public
struct
, which is then used as a type for a
private
member of a
class
.
Even though the
SetHealth
is trivial, it is still worth having.
BUT, one shouldn't blindly have a get and set function for every member variable, the class may as well be a
struct
with default public access. There are several ways to think about this:
* A set function is only really needed if a variable is going to change after it is constructed.
* It may be possible to have one function that does something to an object, changing the value of several member variables, instead of requiring the client to call several functions to individually change the member variables. Say I had a Triangle object, I could a
Move
function instead of requiring the client to call 3 functions to move each vertex.
This all gets back to what
MikeyBoy was saying about class interface (basically public functions that allow the user to interact with the object)
Also I think one problem might be plunging in and writing a bunch of code, then wondering whether it is right or not. Instead, ask your self the questions
MikeyBoy mentioned,
write things down using pencil and paper. Keep it simple, start with just one action the thing can do, make that function as simple as printing something on screen. Also have one member variable.
Here you go, 2 little Exercises:
Only do literally what I have asked for here, it should be simple now, we can add more functionality once you get this down.
Exercise 1:
Create a
class
named
Pistol
.
Specify a default constructor.
Have member function named
Fire
, which prints "Bang!" on the screen.
Create an object of type
Pistol
Call the
Fire
function 5 times.
Exercise 2:
Modify the code from exercise 1, to do the following:
Create a member variable which keeps track of the number of bullets in the Pistol. What type should this variable have?
Make sure this member variable is initialized in a constructor. Provide for a default value of 10.
Write another constructor which allows the user to decide how many bullets the Pistol will have
Every time the
Fire
function is called, decrement the number of bullets variable. Print on screen how many bullets are left.
If there are no bullets left, print a message on the screen, end the program.
We look forward to seeing your code :+)