# Object Oriented question



## GSquadron (Dec 27, 2011)

I have a question regarding C++, but that is an OO question
I learned that making variables private is the best way during the programming.
Now why i need to make the variables private? I don't really understand the reason. 
Anyone pls explain!


----------



## W1zzard (Dec 27, 2011)

Aleksander Dishnica said:


> I have a question regarding C++, but that is an OO question
> I learned that making variables private is the best way during the programming.
> Now why i need to make the variables private? I don't really understand the reason.
> Anyone pls explain!



so that other pieces of your code that have nothing to do with that object can't access the variable and possibly break things


----------



## GSquadron (Dec 27, 2011)

You mean if i have 2 objects with the same name?
Also, if i make a simple program like a calculator or a simple game,
do i need to make the variables always private in the class?
Even if i have a single variable?


----------



## FordGT90Concept (Dec 27, 2011)

You should get in the habit of making everything private at start and only change them to public or add an accessor/mutator if you need them to be public later on.  It's good programming etiquette to do so (isolates classes except for those few places you decide to expose it).


----------



## shuggans (Jan 7, 2012)

Basically, if you have a private variable, it can exists in that subroutine or function.  If it is public, it exists in your entire program.  Here is a dumbed down example:

Public Variable A 
Function 1 {
(some stuff here)
Private Variable B
}
Function 2 {
(some stuff here)
Private Variable C
}

Variable A is public, so it can be called in function 1, function 2, or both.  Variable B is private to Function 1, so function 2 can't see it or call it, and variable C is private to function 2, so function 1 cant see it or call it.

Hope this helps!


----------



## Drone (Jan 7, 2012)

In OO programming every object is just a procedure or function. Every procedure has its own constants and vars. Otherwise it's as if several men use one condom. Who on earth would want to use a public var for example in sum or prod functions. That would mess whole calculations. I think everyone who studies programming knows that from the very beginning.


----------



## xbonez (Jan 9, 2012)

Adding to this discussion, even though he default is private so if you don't explicitly specify an access specifier, the object is private, it is good practice to use the keyword 'private' as it shows intent. Should another developer later work on your code (or even if you were to work on it later), seeing an object marked private means the developer did not intend for this object to be mutated from external classes.


----------



## FordGT90Concept (Jan 9, 2012)

Drone said:


> In OO programming every object is just a procedure or function. Every procedure has its own constants and vars. Otherwise it's as if several men use one condom. Who on earth would want to use a public var for example in sum or prod functions. That would mess whole calculations. I think everyone who studies programming knows that from the very beginning.


No, they aren't.  Objects contain methods, data, and events that describe said object.  For example, a human could have data like the heart rate, height, and weight with methods such as Run(direction), Walk(direction), or Scare().  Events could be things like FellAsleep, WokeUp, or HavingHeartAttack.  All of these are attributes and functions of the human object and all are encapsulated inside the the human object.

Yes, there can be private variables inside of methods but there doesn't have to be.

If the application is running on a single thread, it is possible to use a public variable for functions so long as the value is reset at appropriate times.  I've done this serveral times, multithreaded even.


----------



## Drone (Jan 9, 2012)

^ Objects call procedures  Or you just like to nitpick each and every word?


----------



## FordGT90Concept (Jan 9, 2012)

Objects have subroutines (aka procedures).  Anything with access can call a subroutine.


----------



## yswai1986 (Jan 9, 2012)

private so that other classes/object cannot modify your var other owning object, so that when things goes wrong, THE AUTHOR IS ACCOUNTABLE!


----------

