xscorp7
xscorp7

Reputation: 311

What is the role of private members?

#include<iostream>
class student
{
    private:
            int roll_no;
            int standard;
    public:
            void input();
            void display();
};

I asked my teacher about the significance of making some class members private and some members public. He said that data members are usually made private for security reason. He said that no object can access private things of a class, thats why they are secure.

My question is: When we will develop software, we will be distributing executable files to users. Users will not be able to edit the code. What type of security our teacher is talking about? When I have created the entire code, how someone can edit it? What is the need to think about security?

Upvotes: 2

Views: 1598

Answers (3)

xeco
xeco

Reputation: 184

Just like each person knows their own secrets and it is somehow dangerous to tell others, private members are not exposed to other classes because it may break something in the class and other classes don't really need to know them.

However, people need to communicate to fulfill their needs. We talk, explain our thoughts to be understood.. well, public members are like this, they are needed for the class itself communicate with other classes.

Upvotes: 0

Arnav Borborah
Arnav Borborah

Reputation: 11769

No your teacher would not be correct that encapsulation, as this is called, is for security. Encapsulation is actually there for a few other reasons:

  1. Creates better maintainability of code. When all the properties are private and encapsulated, it is easy for the writers of the code to maintain the program simply by changing the methods.
  2. Have a Controlled Environment. Encapsulation lets the users use the given objects, in a controlled manner, through objects. If encapsulation didn't exist, client code could use the members of your class in any way they wanted, while member functions limit this to a specific behavior.
  3. Hide Complexities: Hiding the complexities irrelevant to the users. Sometimes, some properties and methods are only for internal use and the user doesn't have to know about these. This makes it simple for the user to use the object.

An example that illustrates what would happen if you didn't have encapsulation:

Suppose you had a class called Human, with a member called age that is public. Now, if someone wanted to modify this, say, based off input, then they would have to check to see if the input is not negative or not a huge amount every time, unless they make a function for it. Now if there was a member function instead that provided access to age, then it wouldn't be client code's problem anymore, since the setter for the field would take care of it as it would be the responsibility of the class to make sure its fields are valid.

Upvotes: 8

Mape
Mape

Reputation: 91

This will not affect users of an application, but the teacher is trying to make you safe from your own mistakes.

Keeping member variables private, when possible, protects you from accessing and changing them accidentally from places in your code where you shouldn't do that.

It also makes is clear for the users of the code which variables and functions are intended to be used from outside the class and which are not, thus clearly defining the API of the class.

Upvotes: 3

Related Questions