2
#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?

Blasco
  • 1,607
  • 16
  • 30
xscorp7
  • 303
  • 4
  • 11
  • 1
    C++ classes and their member *visibility* have really nothing to do with security. The `private`, `protected` and `public` keywords are [*access specifiers*](http://en.cppreference.com/w/cpp/language/access) used by the compiler for the code using the class, and nothing more. You should probably [get a couple of good beginners books](https://stackoverflow.com/a/388282/440558) to read. – Some programmer dude Feb 02 '18 at 11:33
  • ... I don't think your teacher is correct. The "private" members are meant to tell programmers that they **should** not access those members. They still can do so if they try, anyway. `*(int*)&student` would probably work in this case. – user202729 Feb 02 '18 at 11:33
  • you mean that the only reason to make members private is to prevent them from being inherited? – xscorp7 Feb 02 '18 at 11:37
  • 1
    "Security" was probably not the best word to use because it has a different main connotation in computing. It is about being safer when writing programs. It is one tool that helps prevent programming errors. – Galik Feb 02 '18 at 11:40
  • Access specifiers have nothing to do with your example in which users can edit source code. The security that your teacher told you about might be misleading, however I think that he is talking about access "security". 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). – Exprove Feb 02 '18 at 11:57

3 Answers3

8

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.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • 1. Can you tell me what is a client code? I am not able to understand your second point. 2. You told that it is also used to hide complexities. But we will be providing executable files to users. Even if there are some functions and variables for internal use, user will not be able to see/access it. Why do we need to hide complexity from user? User doesn't care about the program. – xscorp7 Feb 02 '18 at 11:52
  • 1
    1. Client code is the code that uses the class that you made (Maybe as a library) 2. No, by user I meant the user of the code (i.e the person who is using your class, same as client) not the user of executable. That person and you probably wants to avoid complexities. The actual user of the program only cares for fast programs that work. – Arnav Borborah Feb 02 '18 at 12:14
3

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.

Mape
  • 91
  • 5
  • you mean that the only reason to make members private is to prevent them from being inherited? – xscorp7 Feb 02 '18 at 11:41
  • No...? Did you even read the answer? There are a multitude of reasons to use encapsulation. – Arnav Borborah Feb 02 '18 at 11:42
  • I didn't got it completely. How can somebody accidentally edit the data members even when it is declared under public? – xscorp7 Feb 02 '18 at 11:45
  • @user8051390 Do you know what `private:` does? It stops the progrmmer from changing its value from outside the class. It's not just bout inheritance. – Galik Feb 02 '18 at 11:47
0

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.

xeco
  • 184
  • 14