Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ help
#37
One of the concept of OOP is encapsulation. What it means is that variables/attributes are not being exposed outside of the class and that you would need to manually allow updates via some methods.

The 3 keywords are public, protected, private.

Public methods/attributes can be referred by external objects. It is quite common for methods to be public, so that others can call it, but it is not suggested to have attributes public. IIRC, one of the good use of public attributes is to declare constants. In Java iirc, you can do something like public final SOME_CONST = 1. This allows you to access the constant SOME_CONST directly via the class, which simplify lots of unnecessary steps just to get its value. The danger of having public attributes is that client programs can modify it directly. This leads to a whole world of pains.

Protected simply means only itself or its children can refers to. Protected methods/variables are the bread and butter of inheritance. It is the most flexible keyword of the 3. Most of the times, I made my methods protected so that in the future, I may extends the class without having to modify anything.

Private is the most strict of them all. Only the concrete object can access the private area. If you're sure an attribute or a method is a dead end, make it private.
Reply


Messages In This Thread
C++ help - by DrRusty - 2010-09-21, 10:03 AM
C++ help - by iamflip - 2010-09-21, 10:22 AM
C++ help - by DrRusty - 2010-09-21, 10:41 AM
C++ help - by Eos - 2010-09-21, 11:53 AM
C++ help - by Fiel - 2010-09-21, 12:23 PM
C++ help - by Eos - 2010-09-21, 12:34 PM
C++ help - by DrRusty - 2010-09-21, 12:42 PM
C++ help - by Eos - 2010-09-21, 12:47 PM
C++ help - by DrRusty - 2010-09-21, 12:55 PM
C++ help - by Fiel - 2010-09-21, 12:59 PM
C++ help - by Fiel - 2010-09-21, 01:21 PM
C++ help - by Stereo - 2010-09-21, 01:40 PM
C++ help - by DrRusty - 2010-09-21, 04:23 PM
C++ help - by Fiel - 2010-09-21, 04:35 PM
C++ help - by DrRusty - 2010-09-21, 05:02 PM
C++ help - by Fiel - 2010-09-21, 05:10 PM
C++ help - by DrRusty - 2010-09-21, 05:31 PM
C++ help - by GummyBear - 2010-09-22, 06:21 AM
C++ help - by DrRusty - 2010-09-22, 08:24 AM
C++ help - by GummyBear - 2010-09-22, 09:39 AM
C++ help - by Stereo - 2010-09-22, 01:41 PM
C++ help - by GummyBear - 2010-09-22, 07:15 PM
C++ help - by Fiel - 2010-09-22, 08:50 PM
C++ help - by GummyBear - 2010-09-23, 10:27 AM
C++ help - by DrRusty - 2010-09-27, 09:22 AM
C++ help - by Eos - 2010-09-27, 09:28 AM
C++ help - by DrRusty - 2010-09-27, 09:34 AM
C++ help - by Eos - 2010-09-27, 09:44 AM
C++ help - by Fiel - 2010-09-27, 10:13 AM
C++ help - by Eos - 2010-09-27, 10:27 AM
C++ help - by DrRusty - 2010-09-27, 10:28 AM
C++ help - by Eos - 2010-09-27, 10:34 AM
C++ help - by DrRusty - 2010-09-27, 11:28 AM
C++ help - by ArbalistMaster - 2010-09-27, 01:24 PM
C++ help - by Fiel - 2010-09-27, 03:44 PM
C++ help - by Stereo - 2010-09-27, 04:57 PM
C++ help - by GummyBear - 2010-09-28, 08:57 AM
C++ help - by Russt - 2010-09-28, 07:31 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)