tags:

views:

241

answers:

5

Hi,

Is there any particular reason for having all data members in a class as private by default in C++?

Thanks, Naga

+1  A: 

In a word, encapsulation. The goal is to have private implementation details (such as data members) be private. Only explicitly public API is made available to clients of the class.

Barry Wark
+3  A: 

Because it's better to be properly encapsulated and only open up the things that are needed, as opposed to having everything open by default and having to close it.

Encapsulation (information hiding) is a good thing and, like security (for example the locking down of network services), the default should be towards good rather than bad.

paxdiablo
I do not see how the default access promotes proper encapsulation. This seems to be answering a different question.
Roger Pate
@Roger, it's because people will usually choose the default. And the default in this case is to hide the information. The reason this makes sense is that you have to do extra work to expose things and, because people are inherently lazy :-), this will work to minimise what gets exposed.
paxdiablo
@pax: But as Michael's answer points out, people don't just choose the default most of the time.
Roger Pate
Fair enough, but that's a decision they _make_. Unless they're using some code generation tool which takes that decision away from them, they are explicitly choosing to make things public. In fact, exen if the decision is taken away from them, that too is a decision they have made. If people want to shoot themselves in the foot, that's up to them. All language designers can do is to guide them. Nobody is prevented from using void* for all their pointers in C either but that doesn't make it a good idea.
paxdiablo
+3  A: 

The reasoning is that the public parts of a class should be explicitly made public.

The interesting thing about this (to me anyway) is that the first line after the opening brace of many, many class definitions is public:. Most readers of a class are interested in the public bits, since that's what they interact with, and so many class definitions have their public bits first anyway.

C++'s access specifiers apply to the range that follows them - I think Java and C#'s technique of having each member to specify the visibility of the member (with a sensible default) is preferable.

Michael Burr
And this is the reason I do not see how having public or private by default promotes proper encapsulation at all. It becomes just boilerplate to be confusing once, then glossed over while being repeated endlessly. I'll avoid useless boilerplate instead.
Roger Pate
@Roger: I don't disagree - the fact that class members are private by default is really only conceptually important (if at all).
Michael Burr
Agreed, and apparently you worded it much better than I did.
Roger Pate
+12  A: 

The Design and Evolution of C++

2.10 The Protection Model

Before starting work on C with Classes, I worked with operating systems. The notions of protection from the Cambridge CAP computer and similar systems - rather than any work in programming languages - inspired the C++ protection mechanisms. The class is the unit of protection and the fundamental rule is that you cannot grant yourself access to a class; only the declarations placed in the class declaration (supposedly by its owner) can grant access. By default, all information is private.

Nick D
A: 

Because otherwise there would be no difference at all between class and struct?

Ben Voigt
And what of inheritance, which the question doesn't mention?
GMan
AFAIK `class` and `struct` differ only in the default accessibility. They are identical in terms of all other behavior, including inheritance.
Ben Voigt