Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple public/private keyword in class definition

Tags:

c++

c++11

I have seen multiple public and private keywords in a class definition, like the example below:

class myClass
{
  public:
    void func1(){}
  public:
    void func2(){}
  private:
    int x;
    int y;
  private:
    int z;
    int baz;
};

What is the practical use of this (if any)? Is there any situation in which this could be useful?

like image 576
wizurd Avatar asked Mar 08 '14 05:03

wizurd


People also ask

Can you have multiple public/private protected areas inside a class C++?

There is no limit to how many of these access qualifiers(public, private, protected) you can have in a class. As to why this is useful, it helps writing class declarations the way you want. For example I might want all the member functions (public,protected or private) declared before the (say) private data members.

What is public/private & protected as together called?

The keywords public, private, and protected are called access specifiers. A class can have multiple public, protected, or private labeled sections.

What is public and private in class?

public - members are accessible from outside the class. private - members cannot be accessed (or viewed) from outside the class. protected - members cannot be accessed from outside the class, however, they can be accessed in inherited classes.

What is the purpose of the keywords public and private in the class declaration?

Public, private and protected keywords are used to specify access to these members(properties and methods) of a class from other classes or other .


2 Answers

Is there any situation in which this could be useful?

I can think of a situation where it would be very problematic otherwise:

 class myClass
{
  public:
    void func1(){}
  public:
    void func2(){}
  COORDINATES;    // <-- Note the macro here!
  private:
    int z;
    int baz;
};

which, after the expansion of the COORDINATES macro, becomes:

class myClass
{
  public:
    void func1(){}
  public:
    void func2(){}
  private:
    int x;
    int y;
  private:
    int z;
    int baz;
};

If multiple private / public keywords weren't allowed, it would be very painful to do it. Although using macros isn't good practice; neither is introducing access modifiers silently for all the members appearing after the macro. Nevertheless, it could be useful for RAD tools generating C++ source code.

I can only guess why you see that in human written classes. My guess is that it is poor coding; the writer probably wanted to express that a chunk of data belongs together and / or wanted to be able to move up and down those chunks within the class definition, together with the corresponding access modifier (private/protected/public).

like image 145
Ali Avatar answered Oct 14 '22 08:10

Ali


I'll go one step farther from my comment for this answer, with a snippet of code.

class myClass {

    // initializers etc
    public:
        myClass();
        ~myClass();

    // signal processing
    public:
        void modifyClass();
    private:
        float signalValue;

    // other class responsibilities
    public:
        void doWork();
    private:
        void workHelper();
};

and so on. I wouldn't say this is a solid DESIGN for the class, but it's a good way to show the different capabilities of a class.

like image 1
russellm Avatar answered Oct 14 '22 09:10

russellm