Question about C++ object-oriented design

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
Is there any point in declaring a variable "private" in a class if there's going to be a function to set it to any value and a function to read its value?
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Certainly. The get/set methods offer controlled access. You can put checks in the set value methods to check for illegal values. The get/set methods are especially important when dealing with multiple threads accessing the same data. The get/set methods in that case would use a critical section to manipulate the data without corrupting it.
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
Hmmm I see. But suppose a particular case where I don't perform any value checking in a single threaded app, does it provide any advantage in terms of efficiency or memory?

As always, thanks for the helpful answers singh :)
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
"Is there any point in declaring a variable "private" in a class?.."


Yes, there is.

It is called data hiding. It allows you to use it without knowing the specific data type. Many programmers don't even want to know your underlying implementation, and how that value is stored. All they want to so easily set or read that value.

More importantly, it allows the class to perform any data verification, before actually accepting the specified value. Let's say you have a "Student" class. Now, assume you want to only allow ages of 15-30 to be valid for students. If you simply expose the variable as public, the other programmer (the user of your class) can set it to a NEGATIVE value, or to 12345, or something else. However, if you write a mutator (something to set the value), that method can perform any desired validity checking, such as the value range.

There are other benefits. For example, you may disallow access to certain values when they are not "available". For example, if your object has not been initialized yet, the programmer may try to access one of the public variables and crash the OS, or he/she will get your default value, such as 0. On the other hand, with an accessor you may throw an exception, indicating that the object has not been properly initialized yet, or that it is in an invalid state and the value being requested is unavailable.

Public variables are in general a bad idea. It is recommended to write a mutator and accessor for every value that you need to expose. This applies to both C++ and Java, and I even do it in VB.
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
Originally posted by: ElDonAntonio
Hmmm I see. But suppose a particular case where I don't perform any value checking in a single threaded app, does it provide any advantage in terms of efficiency or memory?

As always, thanks for the helpful answers singh :)

If you're so concerned with performance and efficiency, don't use OOP ;) Classes always slow things down a little bit, and let programmers make mistakes (such as forgetting to perform memory cleanup, allowing for wild pointers, etc). Unless you are the only person using that program, I guess you can go the easy way and do it as a public variable.. But if you ever decide to show up to a job interview and bring that source code, someone may look at you funny =)

It's very easy to write accessors/mutators: just call the variable m_bOldEnough (for a private boolean "Old Enough?"), and call the accessor getOldEnough, and the mutator - setOldEnough. Mostly copy and paste.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: ElDonAntonio
Hmmm I see. But suppose a particular case where I don't perform any value checking in a single threaded app, does it provide any advantage in terms of efficiency or memory?

As always, thanks for the helpful answers singh :)

It also depends on whether the class is for your own use or is more 'professional' :) Well-designed classes should try to limit (or control) access to any 'sensitive' areas so that programmers using your class do not accidentally corrupt the data.

I usually use a 'struct' for simple data storage vs. a class for an 'object'. The 'object's will almost always control access to their data while the 'struct's are dumb and allow the programmer to do as they please.
 

ElDonAntonio

Senior member
Aug 4, 2001
967
0
0
Thanks a lot for your good advice, VBboy and singh!

FYI, I'm actually writing a program (a 3D modeler) partly for fun and partly for my "end of undergrad project" (not sure how to say that in english :) ). I'll put it up online when I have something a little more complete!
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Originally posted by: ElDonAntonio
Thanks a lot for your good advice, VBboy and singh!

FYI, I'm actually writing a program (a 3D modeler) partly for fun and partly for my "end of undergrad project" (not sure how to say that in english :) ). I'll put it up online when I have something a little more complete!

Make sure you comment it well :D
 

VBboy

Diamond Member
Nov 12, 2000
5,793
0
0
Hehe, yes, comment it. DURING the actual programming of the project, not AFTER it is completed and you still have a few minutes left before submitting it ;) A 3d modeler, eh? I did one in QBasic many years ago. It could draw and rotate a 3d surface with proper perspective, etc =)