tags:

views:

89

answers:

7

hello, can somebody please explain the order of private and public is important or no? for example:

class Account{
public:
    Account(string firstName, string lastName, int id);
    void printAccount();
private:
    string strLastName;     
    string strFirstName;    
};

will be the same as:

class Account{
private:
    string strLastName;     
    string strFirstName; 
public:
    Account(string firstName, string lastName, int id);
    void printAccount();
};
+1  A: 

Both express the same.

Till Theis
+5  A: 

It doesn't affect program behavior, but perhaps does affect readability. There's a convention (that you are free to not follow of course) that public stuff must be listed first so that the reader sees the class interface (what other classes and functions can do with the class) earlier.

sharptooth
This convention always frightened me since those who advocate it seem to think that headers should serve as documentation.
Dan Olson
It falls under the category of self-explanatory code. I personally favor the approach because I have found that whenever I found a code with its documentation... the documentation was out of date.
Matthieu M.
+1  A: 

The order is unimportant if you specify the accessibility explicitly. If becomes significant if you don't, because the default is "private".

Philippe Leybaert
@Philippe Leybaert: can You explain please what does it mean?
helloWorld
If you do not use the "private:" or "public:" specifier, then in a class the default is "private".
PeterK
And in a `struct`, it's `public`.
FredOverflow
+3  A: 

In your specific example, the order is not important. Keep in mind however, that members are initialized in the order in which they are declared.

Space_C0wb0y
+1 especially for the last statement, if you don't know this or confuse yourself by having your initializer list in some other order you can get surprised (in a bad way :))
Skurmedel
Last Statement is spot-on. Nice!
Johannes Schaub - litb
+4  A: 

It's important for the visibility of class members

class Account{
public:
    Account(string firstName, string lastName, int id);
    void printAccount();

    Foo makeFoo(); // invalid! Foo not declared yet
    typedef Foo foo_type; // invalid! Foo not declared yet

private:
    string strLastName;     
    string strFirstName;
    class Foo { };    
};

You can only refer to a not yet declared name in a parameter, default argument, function body or constructor initialization list in class (or such things in nested classes). In other cases, you can't and you have to arrange declarations such that they are visible.

Johannes Schaub - litb
A: 

It is just a matter of taste, make up some convention and use is consequently in your application.

Not that private is default so you can omit the private keyword is you declare private variables first.

class Account{
/*private:*/ // can be left out since private is default
    string strLastName;     
    string strFirstName; 
public:
    Account(string firstName, string lastName, int id);
    void printAccount();
};
codymanix
+2  A: 

While in the general case it is more a question of style, there are subtle differences, if you have member attributes with more than one access level, in:

  • initialization order of members
  • (potentially) memory layout of the object

The order of initialization of members in the constructor initialization list is the same order in which the attributes appear in the class definition, so if you place public attributes before private attributes all public attributes will be initialized before any private attribute (this will really affect code only if you use some attribute as an argument to the constructor of a different attribute).

Potentially, the memory layout of the object may differ. All fields with the same access level are lay out in memory in the same order in which they are declared in the class. The compiler is free to reorder different access levels as it wish (i.e. lay out in memory public before protected before private...) but many don't. The memory layout of the object will be different if you reorder the access levels and there are fields in more than one access level --unless the compiler reorders member attributes.

David Rodríguez - dribeas