Why we can't declare public(in C++) two times? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why we can't declare public(in C++) two times?

18th Oct 2017, 10:32 AM
Rushikesh Deshmukh
Rushikesh Deshmukh - avatar
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]
18th Oct 2017, 12:59 PM
Babak
Babak - avatar
+ 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.
18th Oct 2017, 1:32 PM
Babak
Babak - avatar
+ 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.
18th Oct 2017, 10:41 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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 ... };
18th Oct 2017, 10:59 AM
Dennis
Dennis - avatar
+ 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?
18th Oct 2017, 1:15 PM
‎ɐısıօլɐ
‎ɐısıօլɐ - avatar