Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why make class members private?

I've learn C++ for some time, however there is always this question which puzzles me (for years). In school, our lecturers like to declare class variables as private. In order to access it, we have to declare an accessor to access it.

Sometimes we even have to make the different classes become "friends" into order to access its elements.

My question is: Why make it so troublesome? What is the true rationale behind all the private and protected stuff when we can just make our life as a programmer easier by using public for everything?

I was thinking, once the code gets compiled, the end user will not even differentiate whether we use public or private in the back end coding, so why still need to declare it properly as private, protected...etc ?

like image 365
user3437460 Avatar asked Jul 09 '14 19:07

user3437460


People also ask

Why should a class have private members?

These classes have no public setters at all, which guarantees that their objects, once created, will not change their state. This enables a lot of performance optimizations, as well as makes them easier to use in e.g. multithreaded programs, ORM etc. Save this answer.

What is the need for private members?

Placing those variables private should keep the developer away from access mistakes and even improve code readability. In your example any developer that sees your code is aware that he can't access those variables outside the student class, by others word the variables are only known to the student (private).

What does making a class private do?

Private classes are allowed, but only as inner or nested classes. If you have a private inner or nested class, then access is restricted to the scope of that outer class. If you have a private class on its own as a top-level class, then you can't get access to it from anywhere.

Why would you make a class private Java?

In Summary private keyword in java allows most restrictive access to variables and methods and offer strongest form of Encapsulation. private members are not accessible outside the class and private method can not be overridden.


1 Answers

To make your code easier to understand and maintain.

When you make a data member public, other programmers who use your class will naturally tend to use these public members rather than some member function that provides similar or the same information simply because it's easier. You might even tend to do this yourself. That's understandable -- programmers are inherently lazy beasts.

But suppose you realize somewhere down the line that the information that member function provides can no longer be generated from just that one member variable. Worse, what if that member variable becomes obsolete in the face of a design change. Worse still, what if the member variable itself isn't removed, but the semantics of it change.

Let;s consider an example. Say you have a class which represents items you have for sale:

class Gizmo
{
public:
  std::string mSku;
};

That's it, all you have is a sku. Suppose the contents of mSku are simply the product number, something like "12345".

You finish your code, ship it, and life's great. Until your company becomes as successful as Amazon, and now you start getting the same product from multiple vendors. You decide that the best thing to do is to encode the mSku so that it contains vendor information along with the produce number. The same widget from two different vendors might have very different skus: S:12345, Z:12345.

Any code that was written that expects mSku to be just a product number is now broken, and it will all have to be re-factored. In this silly little example it could be a problem. Imagine a codebase of 1 million lines of code -- not uncommon at all. You and all your coworkers have probably forgotten all about all the places where mSku is being used. None of the code will fail to compile, so the compiler's no help -- but all of that code is broken. This is a huge problem.

It would have been better at the outset if you hadn't relied on mSku at all, but provided a member function which was contracted to return the product number. Something like:

class Sku
{
public:
  std::string ProductNumber() const;
private: 
  std::string mSku;
};

Now you can change the semantics of mSku all you want. So long as you refactor what ProductNumber() does to return the product number, all those 1 million lines of code will still compile and still be correct.

In fact, I generally take this one step further. I will generally make everything in a class private until there is a specific reason to make it something else. Even then, I'll only make it protected unless I actually need it to be public.

[Why can't we] just make our life as a programmer easier by using public for everything?

In a large codebase, even if it's only maintained by one person, you actually make your life easier in the long run by making a lot of this stuff private from the very start.

like image 173
John Dibling Avatar answered Oct 04 '22 19:10

John Dibling