Thread: when creating a class, what order do you put things in?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    19

    when creating a class, what order do you put things in?

    i take it is

    private:

    then
    public:



    but is it methods or members first

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Typically, I'll do public, protected, and private, with constructors, functions and data members within these groups.

    It's up to each person's personal preference, but I find that this order makes it easier when using header files as documentation.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's individual preference. I do public, protected, constructors, destructors, operators, member functions, variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I think it's a good idea to put the interface first so that other developers can quickly find what they need. In other words, what Elysia wrote.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I usually put all my member variables at the top, then public, protected & private functions; but if you have a good IDE it probably doesn't matter since you can see the list of member variables & functions on the side bar anyways.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    In C++, the pattern I use is:

    Code:
    class Foo
    {
    public:
        public types (if any: enums, nested classes)
    
        constructors
        destructor
        accessors
        public methods
    
        public variables (if any, rare)
    
    protected:
        protected constructor (if any)
        protected methods
    
        protected variables (if any, rare)
    
    private:
        friend declarations (if any)
    
        private types (if any: enums, nested classes)
    
        private constructor (if any)
        private methods
    
        private variables
    };
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM