public, private, protected...?(C++)
I am by no means an expert or even close to one; but I have been doing regular C for a while. I am fairly new to C++ though (and object-oriented in general) and am very confused about the following:
public, private, protected class the '::' as opposed to just ":" or "." What is the difference and/or explanations of these. thanks. |
Re: public, private, protected...?(C++)
Quote:
As for public et al, I like to use to following analogy. Think of your house as a class. Code:
//this code will NOT compile ;) Private members/methods are like things you put inside your safe. You wouldn't let anybody just barge inside your house and look inside it, heh ? Well private methods just serve that : anything you DON'T want users of that class to use should be marked private. Only other methods of the class can access private stuff. Protected is the middle point : only some parts of your program can access it. What parts, you ask ? - The class itself - Functions/classes marked as friend - Objects that inherit from that class Your TV is an excellent analogy. You wouldn't let anybody and everybody in to watch TV but if a friend came by and asked to watch football, you probably would let him use it. Inheritance and friend functions are another topic entirely. In C++, members are private by default. Interestingly, you can use public, private and protected inside structs in C++, except they are public by default. As for the '::', I was confused by it too. But it's simple, really. Its purpose is to explicit where the function is defined. Look at the following code: Code:
class myInteger { HTH. |
thanks.
|
Remember, when you write a large program, you're gonna end up with many many classes.
Most of the time, *you* are the client -- the user of those classes. When you actually code the classes you're not being a client, but later -- when you actually make use of them in the rest of your program -- you are the client; and you want your life as a client to be as simple as possible. The way you accomplish that is to make your objects simple to use. You want your classes to only make available a small set of easy-to-use member functions -- a set that's small enough that you can quickly make sense of while you're coding another (different) part of your large program. That's what public is for. It's how you "export" the API of the classes that you write. private is for most everything else. The member functions inside your classes that do all the dirty work (they get called by both public and private ones). The ones that you'll pay an awful lot of attention to while you're writing them and getting them to work right, but then later will try to forget about since they will just humm along doing their job behind the scenes. protected is a whole 'nother ball of wax. |
Thanks
Thank you very much SolarBear for your very good and memorable explanation of the public, private, and protected keywords.
|
Quote:
|
Thank you for the advice
Quote:
For anyone looking for a good C++ book, I would highly recommend The Waite Group's Object-Oriented Programming in C++, Third Edition. It's not an "Instant C++ Programming" type book, but is excellent for a reference book and learning more in-depth stuff about C++. It's not hard reading though like that might sound. |
All times are GMT -5. The time now is 08:06 AM. |