Public Group

# Public & Private C++

This topic is 3035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

My friend recently was trying to explain my confusing on the different between something public and protected also known as private in C++. I think they are properly known as a Class and a Struc...

Anyway he explained for a while but i still didn't understand cos he couldn't explain it in a simplified way for me to grasp so im now asking on a forum.

Im trying to find an explaination that can use a real life scenario which shows how the two apply to some thing that way i can understand it bettter in a virtual aspect which is my usual way of approaching my learnings. But this particular one has got me stumped at the moment.

Hope you can help.

##### Share on other sites
I wouldn't call this a difficulty in understanding so much as you haven't even read anything on the material. Instead of asking your friend, get a book, or search online for C++ learning resources. I'd recommend a proper book. Sit down and read about the topics. It's not particularly hard, but you need to invest the time in sitting down and reading and thinking.

##### Share on other sites
Quote:
 Original post by thefollowerBarely readable post...
There is a book online called "Thinking In C++" it will explain OOP very well.

The public and private keywords are used to specify who can access the variable.

Public variables can be accessed outside the class.

Private variables can only be seen and accessed from inside the class itself. You can write functions that can return this value to the outside, or change the value of it.

Example: The code below is used to set the value of an integer, but it's kept within the legal range. If the Number member variable were set to public access, someone could Spinal Tap it to 11, but since it's private, it can only be set by this function, and it's capped at 10.

void ClassName::SetNumber(int x){    if (x > 10) Number = 10;    else if (x < 0) Number = 0;    else Number = x;    return;}

The same for reading variables:

void IPhone4::GetSignalStrength(){    //lolz    //TODO: This properly    SignalStrength = 1 + rnd() % 5;    return SignalStrength;}

##### Share on other sites
We can't really explain it to you without knowing what terms you already understand. We first need to establish some common ground.
What, if anything, do you know about Object Oriented Programming?
What C++ programs have you written?

Or, tell us exactly what you think this stuff means, and ask specific questions about the bits you are unsure of.

One way or another, give us something to go on so that we don't just repeat exactly what you may have read in lots of other places.

##### Share on other sites
Quote:
 Original post by thefollowerMy friend recently was trying to explain my confusing on the different between something public and protected also known as private in C++.

'protected' is not "also known as private" in C++. 'public', 'private' and 'protected' are three separate things.

Quote:
 I think they are properly known as a Class and a Struc...

No. These are two separate concepts.

Quote:
 Im trying to find an explaination that can use a real life scenario which shows how the two apply to some thing that way i can understand it bettter in a virtual aspect which is my usual way of approaching my learnings. But this particular one has got me stumped at the moment.

I would like to try, but I have no idea how you are thinking about these things at the moment.

##### Share on other sites
As others have stated, it's going to be difficult to explain in a way that you can understand, without actually knowing what you understand. It's also difficult to explain in an abbreviated fashion. I strongly recommend getting a book on C++.

Thinking in C++ (free) --> http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

I'll try to briefly explain.

In C++, struct and class are treated almost identically. The only difference is: struct's members (variables) and methods (functions) are public by default. Whereas, with class's members (variables) and methods (functions) are private by default. It is generally considered good practice, I believe, to only use a struct when you're representing data (variables), that don't require methods (functions) to access or modify said data.

You, as the programmer, have the power to declare within your struct or class what members and/or methods are public:, private:, or protected:, regardless of the default imposed.

class Cow{  public:    std::string speak() { return "Moo!"; }    void eat(int foodVolume) { privateEat(foodVolume); }  protected:    void protectedEat(int foodVolume) { stomach = foodVolume; } // used an equal sign because it wouldn't let me use a plus sign, for some reason.  private:    int stomach;};

Now, if we created an instance of the 'Cow' class, we can see what is legal and what will fail at compile time.

Cow *myCow = new Cow();std::cout << myCow->speak() << std::endl; // "speak()" is public, so we call access it.myCow->eat(); // "eat()" is public, so we can access it.myCow->protectedEat(); // Compiler error, because "protectedEat()" is protected and can't be accessed outside of the class, or classes derived from Cow.std::cout << myCow->stomach << std::endl; // compiler error because the member "stomach" is private, and can't be accessed outside of the class.delete myCow;myCow = null;

That's about as brief as it can be explained. Hope that helps some.

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633700
• Total Posts
3013429
×