• Advertisement
Sign in to follow this  

what is the difference between protected and private(c++)?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

what is the difference between protected and private? [edit] Thanks for the help

Share this post


Link to post
Share on other sites
Advertisement
Private means the data declared private within the class can only be altered by specially written "accessor" functions.

Protected means that the data is basically private, but it can be used by derived classes, whereas private data cannot.

HTH,

ukdeveloper.

Share this post


Link to post
Share on other sites
A little side not to add to ukdevelopers comment, protected only works its way down so say you have parent class Animal, and child class Dog and a child class Cat. Only public data can be read by parent clas Animal..not protected or private, even though the class Animal is a parent of Dog and Cat.

Share this post


Link to post
Share on other sites
To make it simple:

Your friends can touch your privates. Your children cannot.

Children and friends know your protected stuff, strangers to not.

Share this post


Link to post
Share on other sites
Quote:
Original post by frob
Your friends can touch your privates.


That's just a little disturbing...

Share this post


Link to post
Share on other sites
Quote:
Original post by ricekrispyw
Quote:
Original post by frob
Your friends can touch your privates.

That's just a little disturbing...

It's also one of the most commonly used descriptions.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement