Class private data member

Hello, I have a question
Should I put all the data types member in private? Is putting data type in public bad?
private is better for encapsulation. http://www.cplusplus.com/doc/tutorial/classes/
Mostly "yes".

Lets have:
1
2
3
4
struct Foo {
  int bar=7;
  // other stuff
};

Two questions:
1. Should the change of bar affect the other stuff? Foo could have a counter to track how many times bar does change. Then there is relevant other stuff and outsiders should not have direct access to bar.
2. As a followup, will bar change? Maybe it turns into a string in next version, but the users will continue to set and get int. Or add that counter. If anyone has had a public access, their code will break.
Should I put all the data types member in private? Is putting data type in public bad?
It depends.

Make it private if the member function of this class use this variable for read/write.
You can make it public if no member functions modify this variable.
@coder777
i think i can see it through your explanations

Make it private if the member function of this class use this variable for read/write.

give me example please

You can make it public if no member functions modify this variable.

give me example please
@keskiverto

1. Should the change of bar affect the other stuff? Foo could have a counter to track how many times bar does change. Then there is relevant other stuff and outsiders should not have direct access to bar.


i understand this one

but could you elaborate more the second one
@giblit
thanks
I tend to approach the issue from the opposite end of most people: I start with the assumption that data members should be public and then see if there is a good reason to make them private.

Reasons for private data include:
- If data members depend on each other.
- If the client code should never change it
- If it's important to maintain binary compatibility through different versions of the code.

Reasons for public data:
- easier to work with
- You can take its address, get a reference, use ++ and --, etc. Note that with private data you could still have a get() method that returns a const reference, but you're trusting the caller not to cast away the const.
As a followup, will bar change? Maybe it turns into a string in next version, but the users will continue to set and get int. Or add that counter. If anyone has had a public access, their code will break.


This is related to giblit's comment, and it happens to be the reason I would say that there should be no public member variables in a class.

I'm not too concerned about changing types, but this is because I am thoughtful about what types I choose in the first place.

Consider a class Person, a Person has an age and a height. An age isn't really a number, it is a Duration. Durations are from one point in time to another, so now we have another type, Date. We'll just leave height as an int for this example.

We might set up our Person as such:
1
2
3
4
5
6
7
8
9
10
11
12
class Person
{
  public:
  Duration age( void ) const { return my_age; }

  void grow( void ) { /* increase my_age and height*/ }
  private:
  
  Duration my_age;
  Date date_of_birth;
  int height;
};


This is all well and good, but what is my_age? Isn't it really the difference between the date_of_birth and now? With proper encapsulation, the user of the class will never know that we stopped keeping track of a person's age:
1
2
3
4
5
6
7
8
9
10
11
class Person
{
  public:
  Duration age( void ) const { return Duration( date_of_birth, Date::now() ); }

  void grow( void ) { /* increase height*/ }
  private:
  
  Date date_of_birth;
  int height;
};
Last edited on
I would take exception to dhayden's response and say that all data should be private unless there is an overriding reason to make it public.

In addition to the advantages already mentioned, there is the following:

- Public variables are akin to globals. Not in the sense of scope, but in the sense that they can be modified from anywhere in your program. Trying to find where a public variable is being changed in a large program can be a nightmare. If the variable is private, you know that is can only be changed within the class and that makes it much easier to set a breakpoint on the few places in the class where it is being changed.

As LowestOne mentioned, sometimes in the development of a large program, I find that a member of a class might start out as a simple variable, but as the program develops, that variable might evolve into its own class. With encapsulation, changing that variable to a class instance is simple and has little if any impact of the rest of the program. If that variable were public, changes could be needed all over the program.

Registered users can post here. Sign in or register to post.