Public or Private in this case?

Let's suppose I have created a GUI calculator, I have these variables
Private one;
Private two;
Private three;
............. etc.

The calculator is an android app. I don't give the source code to anyone except the whole GUI android app which is ready to install.
What will be a problem if I make those variables public? Like:

Public one;
Public two;
Public three;

Regards.....................................................................
Making your variables public or private affects how the compiler treats them at compile time. They have no bearing on your finished binary ready to be given to people.
So, is it a problem how the compiler treats the code for the final output?
My question is, is it a a problem if I make those variables public instead of Private?

If I change private to public and the code compile what is the matter in that case?
The problem there is that by making everything public, it's harder for you (and other people) to understand how to use the classes you write, harder to think and reason about what you're doing, and easier to write bad code that will be buggy and harder to maintain and expand.
Making class members private instead of public is one of the fundamental parts of Object-Oriented Programming: Encapsulation.
https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)

Yes, making something public instead of private won't cause your code to not compile, but it also expands the public interface of your class.

There's a few key points:
1. Classes should be thought of as exposing functionality to a user, not data to a user. These will often overlap, but the primary goal should be functionality. A toaster is not a metal box with resistors and a switch. A toaster is a device that toasts food to make it nice and crispy. I don't care that it uses a resistor or something to produce the burning.
2. Only exposing a public interface allows the maintainer of the class to change the private interface without breaking the code for a user of the class.
3. Only exposing a public interface disallows the user of the class from doing unintended things.
4. Also, data members cannot be polymorphic (virtual), but member functions (methods) can be.

For an example for (3), think about getting the size of a list.
https://en.cppreference.com/w/cpp/container/list/size

Let's imagine that size was just a public data member instead of being a method of list.
The user could do my_list.size = 42;. But... what should that do? You're not calling a function. The size of the list isn't actually 42. By making the actual data that holds the size of the list private, we prevent unwanted accessing of data that should only be manipulated internally by the class.
Last edited on
The problem there is that by making everything public, it's harder for you (and other people) to understand how to use the classes you write, harder to think and reason about what you're doing


I don't give the classes and the source code to anyone. Let's say I make android apps by myself.
Harsh truth about programming. You're going to make mistakes. You're going to look at code you wrote a month before and not understand it. You're going to write a complete mess of code that makes no sense and you can't even begin to debug. These things will happen.

One of the ways to make this less likely is to design your code well. One of the ways to design well is to decide what of your class should be private, and what should be public. Good design helps you understand your own code and helps you write better code, faster.

if you don't want to write good code, if you do want to write buggy messes that don't work, then go ahead and make everything public. If you want to write high quality code, quickly, that you can understand, then design it well and use public and private to help your design.
you are not understanding this at all.
you are thinking that public and private mean something about the user of your program, or other software on the computer, or something.

That is not what it means.

Public and private only have an effect on the code within your own program. They exist to let you control your user defined types to keep what is internal use only and what is exposed to for use apart.

say I have a class that has some values and it has a swap function. The user of my class (which is me, this is all my code) does not care whether I swap it with a temporary variable, the system stack, xor, addition, or some other screwy method. All the user cares is that when swap is called, the values change places. So the 'temp' variable, if I used one, is a good candidate to be 'hidden' from the using code; it does not need to know it exists, it does not need to modify it, print it, or anything. But, they DO want to use the swap method, so that is public.

None of this is seen outside your own code and program, though. Think about the programs you use. If you open your idiot phone and bring up the weather, you don't know that class thunderstorm has a public member 'draw stupid looking icon' in it. You know it can draw the icon, but HOW it does it is hidden, what classes it has inside are hidden, none of that is exposed. The only way to get that info is to get a copy of the source code.

Public and private
these are for scope or design aspect,the user end wont even know that which variables are private or public.
the variables are kept private to ensure we dont accidentally mess things up or have a tough time messing things up,also when you share your modules externally you give them acess to publlic functions that would make your module have proper flow of to set it up,
if we make erythingn public its just makes no sense,
public,private these are meant to be a part of Object Oriented pillars
Abstraction and encapsulation

Abstraction
you only expose what you want to allow
We do this using functions and making the data private

encapsulation
you need to do a task but how it does shouldnt be a users concern on how you do it
encapsulation is because tomorow code will always be changed and made more efficient,so the user should never know how the code is programmed



Well, thanks for your explanation.
Topic archived. No new replies allowed.