LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-09-2004, 11:23 AM   #1
name_in_use450
Member
 
Registered: Jun 2004
Location: United States
Distribution: slackware 10.0 mostly; used many
Posts: 109

Rep: Reputation: 15
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.
 
Old 08-09-2004, 01:17 PM   #2
SolarBear
Member
 
Registered: Oct 2003
Location: Québec, Canada
Distribution: Gentoo 2005.0(desktop), Debian 3.0 r2 (server)
Posts: 105

Rep: Reputation: 15
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.

Last edited by SolarBear; 08-09-2004 at 01:19 PM.
 
Old 08-09-2004, 07:20 PM   #3
name_in_use450
Member
 
Registered: Jun 2004
Location: United States
Distribution: slackware 10.0 mostly; used many
Posts: 109

Original Poster
Rep: Reputation: 15
thanks.
 
Old 08-09-2004, 08:39 PM   #4
johnMG
Member
 
Registered: Jul 2003
Location: CT, USA
Distribution: Debian Sarge (server), Etch (work/home)
Posts: 601

Rep: Reputation: 32
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.

Last edited by johnMG; 08-09-2004 at 09:00 PM.
 
Old 10-06-2008, 10:48 PM   #5
rlhawk
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Rep: Reputation: 0
Smile Thanks

Thank you very much SolarBear for your very good and memorable explanation of the public, private, and protected keywords.
 
Old 10-07-2008, 01:56 AM   #6
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by rlhawk View Post
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++.
 
Old 10-07-2008, 11:43 PM   #7
rlhawk
LQ Newbie
 
Registered: Oct 2008
Posts: 3

Rep: Reputation: 0
Thank you for the advice

Quote:
Originally Posted by ErV View Post
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.
 
  


Reply

Tags
c++, classes, protected, reference



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh public/private keys lord_darkhelmet Linux - Newbie 8 10-29-2005 03:14 PM
public vs private ip emailssent Linux - Networking 2 09-28-2004 02:11 AM
Linking Public IP to a Private IP Saints Linux - Networking 0 05-04-2004 04:33 AM
Public Windows, Private Linux c0rrupt0 Linux - Networking 8 09-11-2003 11:48 PM
Help with SSH and public/private keys stodge Linux - Security 5 05-14-2003 01:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration