Is private member declaration necessary in a Class?

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.
Last edited on
You're right and wrong in this post.

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.
Last edited on
> Actually a base class doesn't pass down private members to a derived class
You can't access them, but they are there.
I prefer to put the public stuff first and the private at the end.
1
2
3
4
5
6
7
class Example
{
public:
	void fonc();
private:
	void func();
};

If I hadn't used private here func would have been public.
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.
ah i got protected and private mixed up there, i had forgotten private was only to friends not derived, solved my main question though :)
Topic archived. No new replies allowed.