LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   public, private, protected...?(C++) (https://www.linuxquestions.org/questions/programming-9/public-private-protected-c-215399/)

name_in_use450 08-09-2004 11:23 AM

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.

SolarBear 08-09-2004 01:17 PM

Re: public, private, protected...?(C++)
 
Quote:

Originally posted by name_in_use450
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.

A class, very simply put, is a struct : it contains variables (which are called members in this contexts) and functions, called methods. However, it's how you access them that really makes a change.

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 ;)
class House {
public:
  mailbox myMailbox;
protected:
  tv myTV;
private:
  safe mySafe;
};

Think about it. Anybody, friend, foe, or even an unknown person can use your mailbox, put stuff in it, or take stuff from it (well that wouldn't be very honest, but hey.) Public members/methods can be accessed by anything, from any context.

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 {
private:
  int myInt;
public:
  int changeInt(int x);
};

int myInteger::changeInt(int x) {
  myInt = x;
}

The last lines mean "define myInteger's changeInt method". Otherwise, your compiler would think it's a simple function, unrelated to your class.

HTH.

name_in_use450 08-09-2004 07:20 PM

thanks.

johnMG 08-09-2004 08:39 PM

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.

rlhawk 10-06-2008 10:48 PM

Thanks
 
Thank you very much SolarBear for your very good and memorable explanation of the public, private, and protected keywords.

ErV 10-07-2008 01:56 AM

Quote:

Originally Posted by rlhawk (Post 3302511)
Thank you very much SolarBear for your very good and memorable explanation of the public, private, and protected keywords.

I recommend to read books on C++, especially parts about classes and inheritance. public/private/protected are normally explained in detail in every self-respecting book about C++.

rlhawk 10-07-2008 11:43 PM

Thank you for the advice
 
Quote:

Originally Posted by ErV (Post 3302626)
I recommend to read books on C++, especially parts about classes and inheritance. public/private/protected are normally explained in detail in every self-respecting book about C++.

Thanks for the advice. I agree fully, and (for what it's worth) Everybody: Take ErV's advice! Don't depend on forms to learn in detail about stuff like this. Nobody can quickly post an explanation of class inheritance and all the stuff you'll need to know it. I actually have read a huge amount of stuff about C++ ...Started learning it when I was 13 actually. Now, 5 years later, I've moved out of home, and no longer have access to my Dad's huge library of programming books. I was just looking for the answer to this question for reference purposes because I haven't touched my code in quite a while and wasn't remembering exactly what the protected keyword in some of my classes meant. Been working with php and web development too much lately... I don't have as much time for working on my C++ game any more. :(

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.