+ 4
Why we can't declare public(in C++) two times?
5 Answers
+ 22
-1 for hatsy! (Sorry but misleading answer)
As a dumb but completely valid example, you'd do such things.
class A {
public:
int a;
private:
int b;
public:
int c;
protected:
int d;
public:
int e;
private:
int f;
};
int main()
{
//...
}
[http://cpp.sh/9ovxf]
+ 19
As you see nowadays, lots of learners tends to implement obfuscated codes rather than well-organized and readable ones (which highly advised by gurus). Maybe the Asker wanted to know about that. In addition to that, being curious about unknowns is an asset for learners to leap to the next level.
+ 12
Because this is how access specifiers work in C++. You specify public keyword once and all class members under public are, well, public. Similarly, private keyword is only used once in a single class declaration.
+ 6
?
We can use public/protected/private as many times as we want inside a struct/class.
Personally I like to seperate nested class', typedefs, functions and member variables like
class A
{
public: // Nested class's / typedefs
class Nested
{
...
};
public: // public functions
...
private: // private functions
...
private: // member variables
...
};
+ 4
like they said it is possible, but a lot easier to write it just once, preferably you don't even write private because if nothing is written there it is private by default, and then you write public once and put everything there which needs to be public, why make your life complicated?