Reputation: 1192
This might not be the greatest question but since classes in c++ have all their attributes and methods by default private is there any point in specifying it?
Is it just a matter of preference? Does specifying the private part make the code look more or less proficient?
class User
{
string name;
string surname;
int miles;
double balance;
public:
User(string,string,int,double);
};
vs
class User
{
private:
string name;
string surname;
int miles;
double balance;
public:
User(string,string,int,double);
};
Upvotes: 6
Views: 1669
Reputation:
It's all stylistic and a matter of preference.
For example, in my team, we prefer to have publics before privates given that people generally read from top to bottom, and more people will be interested in how a class is used rather than how its implemented (like most people are interested in driving a car more than learning how a combustion engine works). So there we are forced into using private
anyway given that we list those on the bottom, unless we used a struct
where the default access is public (in which case we could omit the public
specifier, but we typically don't use structs
to save typing so much as to indicate simple aggregates of data).
But that's just how we roll. It's all up to you. Some may find the explicitness of seeing a private
specifier beneficial to avoid mistakes in having to assume the default access of a class
, e.g. It really varies on your team dynamics, what you want to emphasize, etc.
I think the only not-so-subjective aspect of a stylistic preference is that if there's one style that's superior, it's merely any style followed consistently by your team. If there's anything to ultimately achieve with a stylistic standard, it's merely that: consistency. This is especially true if you are publishing headers to the rest of the world (ex: as part of a software development kit). There you don't want each header to look like it was written by a completely independent person following a different set of standards. So sometimes a very 'exotic' standard with a lot of special rules and exceptions might be worth avoiding, no matter how arguably cool it is, if your team has a hard time consistently following it. Or they may not. It all depends.
Upvotes: 7
Reputation: 11163
May increase readability. Some other language like java it have to explicit to make a class member private
. Because there is a access modifier named default
- when we don't write any access modifier before a class member. So it would be easier for those coming from other programming language to understand if we make explicit the access modifier - private
. And of course it is a matter of preference in C++
.
Upvotes: 3
Reputation: 385174
It is just a matter of preference.
struct Foo
{
public: // These are already public; no need to write it
void a();
int b;
private: // You need this one though
char c;
};
Similarly:
class Bar
{
private: // These are already private; no need to write it
void a();
int b;
public: // You need this one though
char c;
};
Upvotes: 1