Can there be two public section in a class? If yes then why ? And in which circumstances we do so?

Abhineet picture Abhineet · Apr 27, 2010 · Viewed 10.3k times · Source

There is something bugging me about classes. For example

class A
{
public:
  A()
  {
  .....
  .....
  }

  void cleanup()
  {
  ....
  ....
  ....
  }

public:
  UINT a;
  ULONG b;
};

In the above example there are two public section. In the first section I am defining a constructor and a method and in the second section I am declaring data members. Is the above class i.e. A correct. Can we do that? If yes then why is that needed and in what circumstances should we use it? Since we can do the entire thing in one section then why are there two sections?

Answer

Marcelo Cantos picture Marcelo Cantos · Apr 27, 2010

Access qualifiers simply apply to the code that follows until the next qualifier. There is no restriction on the number or order of such qualifiers.

It is generally not necessary to repeat the same access qualifier in a class, and doing so is likely to confuse the reader. They also may have an effect on the layout of a class, since data members following the same qualifier must be laid out in the order they are declared, but there is no such restriction between qualifiers.