• 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,351 (0.64/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:
 

W1zzard

Administrator
Staff member
Joined
May 14, 2004
Messages
27,046 (3.71/day)
Processor Ryzen 7 5700X
Memory 48 GB
Video Card(s) RTX 4080
Storage 2x HDD RAID 1, 3x M.2 NVMe
Display(s) 30" 2560x1600 + 19" 1280x1024
Software Windows 10 64-bit
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
 
Joined
Dec 2, 2009
Messages
3,351 (0.64/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
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

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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).
 
Joined
Dec 22, 2007
Messages
184 (0.03/day)
Location
Central Nebraska
System Name CORSAIR
Processor AMD Phenom 1090T x6 @3.2
Motherboard Gigabyte Ga-78LMT-S2P
Cooling High efficiency dust cooling
Memory 8GB GSkill DDR3 1333
Video Card(s) Sapphire Radeon HD3870 512MB GDDR4 PCI-e toxic
Storage Seagate SV35.3 ST3250310SV 250GB SATAII(Windows 7 Pro x64), 320GB Samsung SATAII(Storage and SuSE)
Display(s) Dell 20" LCD, Dell 17" LCD
Power Supply Antec Cool Blue 650W Modular
Software Windows 7 Professional SP1, Visual Studio 2010 Ultimate
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!
 
Joined
Sep 1, 2010
Messages
7,023 (1.41/day)
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

New Member
Joined
Nov 29, 2010
Messages
1,182 (0.24/day)
Location
Philly, PA (US)
System Name Winter
Processor AMD Phenom II x4 965 BE @ 4.0Ghz
Motherboard MSI 790FX-GD70
Cooling Corsair H50 Liquid Cooling
Memory 2 x 2Gb Gskill Ripjaws 1600Mhz (7-7-7-24@1.6V)
Video Card(s) Asus GTX 470 @ Stock (Zalman VF3000 cooler)
Storage 2 x Samsung Spinpoint F3 500GB (RAID 0)
Display(s) Hanns G 28" @ 1920x1200
Case Antec 1200
Audio Device(s) Onboard -- TosLink --> Z5500
Power Supply Corsair 850TX 850W PSU
Software Win 7 64-bit Ultimate
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

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
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.
 
Joined
Sep 1, 2010
Messages
7,023 (1.41/day)
^ Objects call procedures :rolleyes: Or you just like to nitpick each and every word?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.63/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Objects have subroutines (aka procedures). Anything with access can call a subroutine.
 

yswai1986

New Member
Joined
Jan 9, 2012
Messages
3 (0.00/day)
private so that other classes/object cannot modify your var other owning object, so that when things goes wrong, THE AUTHOR IS ACCOUNTABLE!
 
Top