• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Object Oriented question

Joined
Dec 2, 2009
Messages
3,353 (0.59/day)
System Name Dark Stealth
Processor Ryzen 5 5600x
Motherboard Gigabyte B450M Gaming rev 1.0
Cooling Snowman, arctic p12 x2 fans
Memory 16x2 DDR4 Corsair Dominator Pro
Video Card(s) 3080 10gb
Storage 2TB NVME PCIE 4.0 Crucial P3 Plus, 1TB Crucial MX500 SSD, 4TB WD RED HDD
Display(s) HP Omen 34c (34" monitor 3440x1440 165Hz VA panel)
Case Zalman S2
Power Supply Corsair 750TX
Mouse Logitech pro superlight, mx mouse s3, Razer Basiliskx with battery
Keyboard Custom mechanical keyboard tm680
Software Windows 11
Benchmark Scores 70-80 fps 3440x1440 on cyberpunk 2077 max settings
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! :wtf:
 
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! :wtf:

so that other pieces of your code that have nothing to do with that object can't access the variable and possibly break things
 
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?
 
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).
 
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!
 
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.
 
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.
 
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.
 
^ Objects call procedures :rolleyes: Or you just like to nitpick each and every word?
 
Objects have subroutines (aka procedures). Anything with access can call a subroutine.
 
private so that other classes/object cannot modify your var other owning object, so that when things goes wrong, THE AUTHOR IS ACCOUNTABLE!
 
Back
Top