| Author |
Message |
Scott tutorialtoday.com

Joined: 24 Mar 2005 Posts: 2650 Location: Mississauga, Ontario
|
Posted: Thu Mar 06, 2008 9:06 pm Post subject: Question about C++ Classes |
|
|
I am wondering what the point of using public, private, and protected variables and/or functions in a class. I understand how they work and everything, just not the reasoning.
I mean, is there any special benefit in having a variable set as private then having a public function which you call to retrieve them, instead of just setting everything to public and accessing it directly? (this is just based on tutorials and code examples I have seen) _________________ Tutorial Management Script - Version 1.4 Released
TutorialToday - Up and running, submit your tutorials!
Linux Tutorials - Coming Soon |
|
| Back to top |
|
| |
mcwkm Grandmaster Poster

Joined: 30 Mar 2005 Posts: 323 Location: ct
|
Posted: Thu Mar 06, 2008 9:56 pm Post subject: |
|
|
| having a variable set to private makes the user not able to change the variable, adding a public function to retrieve the variable just gives them its value but does not let them change the variable |
|
| Back to top |
|
| |
krt ...

Joined: 11 Jan 2005 Posts: 4765 Location: Down Under
|
Posted: Fri Mar 07, 2008 1:15 am Post subject: |
|
|
mcwkm, I think he knows that but wants to know what the point is.
Scott, effectively, you could argue there is no point, in the same way you could argue against interfaces and abstract classes. The benefits are mostly in enforcement and can be useful for teams where there may be ambiguity as to what shouldn't be accessed from outside and what should. Basically, their purpose is to keep things organised promoting extensibility and maintainability.
Of course, the tutorials you see with those crude getter/setter methods for private variables give the wrong idea and I have no idea why tutorials use them the way they do, especially in abundance. The idea is to keep data in the class and return information (note the difference between data and information).
If you are interested in OOP, I strongly recommend a good book (as opposed to other aspects of development where tutorials can suffice at times). "Code Complete", "Thinking in Java" and "Design Patterns : Elements of Reusable Object-Oriented Software" are ones that come into mind. The first one is especially useful in getting across the concepts of OOP. The second one has a free ebook version and despite its name, it is not all specific to Java, the OOP related sections apply in general to languages. |
|
| Back to top |
|
| |
Pie32 Not Banned

Joined: 17 Mar 2005 Posts: 1443 Location: Lost in 84
|
Posted: Sat Mar 08, 2008 6:19 pm Post subject: |
|
|
| mcwkm wrote: | | having a variable set to private makes the user not able to change the variable, adding a public function to retrieve the variable just gives them its value but does not let them change the variable |
This can then prevent problems while programming. For instance, if someone accidentally uses the assignment operator instead of the equals operator.
Accessing the variable directly will compile just fine, but may cause unexpected behavior in the code:
| Code: | | if (myClass.var = 5) // always evaluates to true, and sets var to 5 |
On the other hand, using a public getter will cause a compilation error if the assignment operator is used:
| Code: | | if (myClass.getVar() = 5) // compilation error |
Also, sometimes you will have variables in your class that are for internal use only, and you don't want anything else to ever be able to touch it, let alone even know it exists. _________________ [img]http://luneknight.com.ru/counter.jpg[/img]
Random Battle: [img]http://luneknight.com.ru/l.jpg[/img] vs. [img]http://luneknight.com.ru/r.jpg[/img] |
|
| Back to top |
|
| |
Scott tutorialtoday.com

Joined: 24 Mar 2005 Posts: 2650 Location: Mississauga, Ontario
|
Posted: Sun Mar 09, 2008 8:18 pm Post subject: |
|
|
| krt wrote: | | If you are interested in OOP, I strongly recommend a good book (as opposed to other aspects of development where tutorials can suffice at times). "Code Complete", "Thinking in Java" and "Design Patterns : Elements of Reusable Object-Oriented Software" are ones that come into mind. The first one is especially useful in getting across the concepts of OOP. The second one has a free ebook version and despite its name, it is not all specific to Java, the OOP related sections apply in general to languages. |
What about good books for C++ in general... since there doesn't seem to be too many resources online. _________________ Tutorial Management Script - Version 1.4 Released
TutorialToday - Up and running, submit your tutorials!
Linux Tutorials - Coming Soon |
|
| Back to top |
|
| |
exsanguination Forum Regular
Joined: 27 Apr 2005 Posts: 418 Location: Australia
|
Posted: Wed Mar 12, 2008 6:48 pm Post subject: |
|
|
| Scott wrote: | | What about good books for C++ in general... since there doesn't seem to be too many resources online. |
Thinking in C++ by Bruce Eckel.
http://mindview.net/Books/TICP.....CPP2e.html
p.s. krt, Design Patterns is an awesome book, I have a copy sitting on my desk right now (right next to "Refactoring: Improving the Design of Existing Code" which is also highly recommended)  |
|
| Back to top |
|
| |
|
|
|