I dont know if im mistaken but classes automatically set their members to private, as in only objects from that class and inherited classes can use them, so why even declare private members? is there any difference from
1 2 3 4 5
class Example
{
private:
void func();
};
than
1 2 3 4
class Example
{
void func();
};
this would also apply with structs making their members automatically public.
Am i mistaken with something? Is there any reason to declare private members in a class asides from readability? Not exactly a problem, i just like to be sure whenever theres a doubt i cant find an answer to online.
1.) Yes, classes by default do make their members and functions private
2.) Actually a base class doesn't pass down private members to a derived class
Yeah, with a class any members declared before your public and/or protected members would be private, even without private: before hand. I usually still put it there anyways for the heck of it. Same goes for structs except as you already know structs by default have their members public. Your two code examples will have the same exact results.
I prefer to put the public stuff first and the private at the end.
Yeah, this is the usual convention. If someone's looking at your class definition, it's almost always because they want to use your class, which means their main interest is in the public interface. Putting the public stuff at the top makes it easier for people to find.