What's the use of making instance variable private if we compile the program?

Hi.
Why use private variables inside a class? If the program compiled so, it isn't the same if they public or private? I don't mean a library I mean a complete program.
On the lowest level it is actually the same.

Access specifier allows the programmer to control the use of the variables. They are used in different scenarios.

It is also a safety mechanism. The less accessible the more safe.

The reasons for the use of access specifier apply to the whole program, it does or should not end at the border of libraries.

If the program compiled so, it isn't the same if they public or private?

But it won't compile, will it? If you make a variable private, and then you try to use it, the compiler will stop.

The finished code will be the same; private/public stops the programmer making mistakes and helps the programmer organise the code and communicate forwards to other people who will have to maintain or interact with the code.

Private/public is for you, to tell the compiler what you meant so it can stop you breaking your own design.
Last edited on
It is also a safety mechanism. The less accessible the more safe.
What do you mean? You mean from hackers or decompilation?

If the program compiled so, it isn't the same if they public or private?
But it won't compile, will it? If you make a variable private, and then you try to use it, the compiler will stop.
It compile why not? If it's public or private it compile.
Thanks you both.
What do you mean? You mean from hackers or decompilation?

Safe from you and from other people who will work on the code. You'll make mistakes. You'll do things wrong. There are ways you can reduce those mistakes. One such way is to make variables private when appropriate.

private variable means "DO NOT DIRECTLY ACCESS THIS VARIABLE". It is a message to yourself and to everyone else who will ever work on the code.

It compile why not? If it's public or private it compile.

No.

This will compile:

1
2
3
4
5
6
7
8
9
10
11
class someClass
{
  public:
    int value; // Go ahead, access this variable
};

int main()
{
  someClass instance;
  instance.value = 7;
}

This will not:
1
2
3
4
5
6
7
8
9
10
11
class someClass
{
  private:
    int value; // DO NOT DIRECTLY ACCESS THIS VARIABLE
};

int main()
{
  someClass instance;
  instance.value = 7; // Try to access the forbidden variable
}
Last edited on
private variable means "DO NOT DIRECTLY ACCESS THIS VARIABLE". It is a message to yourself and to everyone else who will ever work on the code.

No one works on the code, I don't give the code to anyone, I don't mean creating libraries, I mean a complete program, for example I will create a program like a photo editor or a game.
Sounds like you're sure you will never make any mistakes, so you might as well just make everything public.

Private variables are better for programmers who might have to work with other people, or whose code might be maintained or continued by someone else, or who might have complex designs, or flexible code behind contracted APIs, or will be working on code for long periods of time. If it's just you making simple toy programs and you know you don't make mistakes, you don't need them.
Last edited on
I think the point is the compiler is your partner. Help it, and it will help you. Whenever the compiler issues a warning message or an error message, it is a sign that it is doing its job and reminding you of your own design intentions.

Even if the entire code is completed within a fairly short period of time, so all of the little foibles and idiosyncrasies can be kept in one's head, there is a good chance that you may want to return to the code whether in six months or three years time, to modify or add extra features. Eventually even the program's own author will have difficulty remembering every small detail or even the large details. Writing code which effectively documents how a class is intended to be used saves a lot of problems later on.
Why use private variables inside a class?

You’re not the only one who asks this question. Have you already tried to read some good article on Internet? Perhaps there isn’t a definitive answer, but you should decide according to your specific code.

A property should be private if you don’t want it to be easily modified.

There’s likely to be no reason to make everything private:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-get
C.131: Avoid trivial getters and setters
Reason
A trivial getter or setter adds no semantic value; the data item could just as well be public.
Editors: Bjarne Stroustrup, Herb Sutter

If you look at the example of what are intended to be trivial getters and setters, you can see they let the private property being freely modified by the class user, so there’s no difference between
1
2
3
4
5
6
7
class MyClass {
public:
    int getMyInt() const { return myint; }
    void setMyInt(int myint_arg) { myint = myint_arg; }
private:
    int myint {};
};

and
1
2
3
4
5
class MyClass {
public:
    int myint {};
private:
};

In both codes you can assign to the variable “myint” all the values you want, with no restriction.

But usually that’s not what you want for pointers. Pointers
- should be guaranteed to point to an available memory area before been assigned any value;
- if you change the value of a pointer (making it point to a different memory area), you often, but not always, want to delete of free() the previous memory area.
So you’re likely to write a method to set them anyway, both if they are public and private. If so, why keeping them freely accessible, increasing the possibilities to make some mistakes?

I’ve thought for a length to sensible examples for helping you to take some decisions on when you want your properties to be private and when public, but I can’t but admit the ones you can easily find yourself on Internet are far better then mine. But, just to keep it easy, you could think about a situation like this:
Let’s assume you have a simple integer property, like the “int myint;” above; but in your program this int must remain inside a range of values, let’s say no less than 0 and no more than 20. If so, wouldn’t it be simpler to manage it by a method which check the boundaries before assigning the new value? Otherwise you’ll be forced to remember this limitation while writing every single function, even if you had to take a few month pause on that code, which would likely cause you to forget everything about it.
And if you write a method to manage it...

I think the most voted answer here
https://stackoverflow.com/questions/6528499/is-it-okay-to-forgo-getters-and-setters-for-simple-classes
can be kept as an acceptable rule of thumb for this problem.
Appreciate your informative comments, thanks.
Topic archived. No new replies allowed.