ok so i am having this little confusion in c++, and i know that it might sound a little stupid to u guys after u read it..bt anyways here it goes..
We create private variables coz we dont want anyone to mess with our programs and coz thats a good programming practice..(thats wat sir bucky said). But then we create a public function or variable and it just gives the user some privilege to change the private variable, which cud not have been done earlier..
so my doubt is :
WHY THE HELL DO WE EVEN CREATE THE PRIVATE OR PROTECTED VARIABLES IF ULTIMATELY, WE ARE GIVING THE USER THE POWER TO CHANGE THEM AS THEY WANT..!!
so thats my little concern..hope u guys will hop onto this wierd problem soon..thanks all!!
and just to be clear..i am a newbie..and this seemed like a valid confusion..so i posted it..
PUBLIC , PRIVATE and PROTECTED
Post a Reply
|Oldest Newest Rating|
· October 21, 2014
As far as I know, in general programming, a public variable just means that the variable is usable source-code wide, e.g. accessing it _directly_ via different source code files, or at least accessing it inside a function even the var is outside of it. Depends on language I guess.
In context of OOP / Classes, a protected variable is only accessible [directly] inside the Class (and it's extending classes, if any). What Bucky may have meant is that we should keep them protected - and make a function inside a Class that will return the variable (or set new value). This is very handy if we want a specific var to stay inside the Class scope. That would be sort of indirect way of retrieving it / setting it - a more safe way.
If I am wrong, I'd like you to correct me!
You might do a bit Googling on 'Variable scope'.
Public, private and protected variables should be of much benefit if used properly.
· October 20, 2014
because in c++ you can change how you access it, by using pass by reference, value or pointer
Used in many types of software including music players, video games, and many large scale applications.
|Bucky Roberts Administrator|