I'm having trouble understanding the difference between private and protected members in a C++ class. In simple terms, what is the difference?
3 Answers
protected
members are accessible by derived classes. private
members are not.
Generally (most of the time) members should either be private
or public
. It is rare and unusual to need a protected
member (edit) in a well-designed system.
EDIT:
Maybe I should elaborate on why protected
members can be a code-smell.
If derived classes have access to data members that other classes do not, this could be an indication that the base & derived classes are too tightly coupled. The derived classes have access to the base class' state, and therefore the base class' state is subject to corruption. If this were not the case, then there's also often no reason to just make the data members public
.
Others have gone in to greater detail on this.
Here is what Stroustrup says in his text:
Members declared protected are far more open to abuse than members declared private . In particular, declaring data members protected is usually a design error. Placing significant amounts of data in a common class for all derived classes to use leaves that data open to corruption. Worse, protected data, like public data, cannot easily be restructured because there is no good way of finding every use. Thus, protected data becomes a software maintenance problem.
See also this question.

- 1
- 1

- 99,718
- 31
- 186
- 324
-
What is not-well-designed about use of `protected` members? (I use them all the time--what am I doing wrong?) – Kristopher Johnson Jan 12 '11 at 19:04
-
@Kristopher: See my edit. I have elaborated and cross-referenced in an attempt to answer your question. – John Dibling Jan 12 '11 at 19:13
-
In http://www.gotw.ca/publications/mill18.htm, Sutter says "if derived classes need to invoke the base implementation of a virtual function, make the virtual function protected". So that's one expert on your side and one on mine. :) But seriously, I just think "rare and unusual" is an overstatement. I'd accept "unusual", but sometimes accepting some coupling between base and derived classes is simpler than the alternatives. – Kristopher Johnson Jan 12 '11 at 19:22
-
I don't think anything I said disagrees with Sutter's assertion that if you need something to be `protected`, then it should be `protected`. But note also that even Sutter said, in the very article you linked, that those same virtual methods should be `private` "by default". – John Dibling Jan 12 '11 at 19:26
-
I think we basically agree; we just have different connotations for the word "rare". – Kristopher Johnson Jan 12 '11 at 19:29
-
Re-reading your answer, I now notice that your elaboration is about _data_ members. I wholeheartedly agree that data members should never/rarely be made `protected`. My comments are really about function members. – Kristopher Johnson Jan 12 '11 at 20:07
-
Yes, I was talking about *data* members. Though I was not clear about that. – John Dibling Jan 12 '11 at 20:51
Protected members can be accessed by derived classes (and friends).
Private members can only be accessed by the declaring class (or by friends).
Simple example:
class Base
{
protected:
int prot;
private:
int priv;
public:
int Prot() const { return prot; }
int Priv() const { return priv; }
};
class Derived
{
public:
void ShowProt() { cout << prot; } // OK - prot is accessible because it is protected
void ShowPriv() { cout << priv; } // Compile Error - cannot access priv, which is private
void ShowPriv2() { cout << Priv(); } // OK, because Priv() is public
};

- 81,409
- 55
- 245
- 302
From the C++ FAQ:
- A member (either data member or member function) declared in a private section of a class can only be accessed by member functions and friends of that class
- A member (either data member or member function) declared in a protected section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes
- A member (either data member or member function) declared in a public section of a class can be accessed by anyone

- 17,657
- 2
- 26
- 34