Confused on Private and Public Member Variables

closed account (3voN6Up4)
Hey guys, I am very confused on when I should be using the "private:" and "public:" in classes. I normally use structures which make all member variables/functions public, but I have been told it's good to always use classes no matter what and just do "public:" for the things that you want public. Kind of a security thing, you know?

Why would you make it private? What's the point? You can't do anything with the private variables or link to them in any way shape or form.

In this code example below, I have Weight and Height private, but you cannot even see them. It seems like it's just a waste of space to have things private. Can anyone elaborate?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
class Person
{
    double Weight = 314.78;
    double Height = 6.7;
public:
    int Age = 18;
};
int main()
{
    Person Lucas;
    cout << "Age: " << Lucas.Age << endl;
    cout << "Weight(lbs): " << Lucas.Weight << endl;
    cout << "Height(ft): " << Lucas.Height << endl;
}
If all you are using your class for is contained a set of variables you want to modify directly, then yes, private members are useless. But if you class is complex at all you will likely have some internal state that you manage that the user of the class does not and should not need to worry about.

You may consider, for example, std::vector. There is a lot of internal memory management that goes that you are not allowed direct access to when just manipulating an instance of the class.
Making variables private is part of data encapsulation.
http://www.tutorialspoint.com/cplusplus/cpp_data_encapsulation.htm

Imagine you have a large program that accesses Person's variables willy-nilly. You have no control over how your variables are accessed or changed. What if some piece of code sets Age negative? How do you find it?

With encapsulation, you create a well defined interface for accessing member variables. The simplest interface are getters and setters for each of the variables. Public getters and setters that simply retrieve and store the value are only a slight improvement over public variables. At least you can set a breakpoint in your setter and see where it is being called when it's value gets changed unexpectedly.

The advantage to providing a well defined interface is that you are free to change the implementation of your variables. For example, you may decide that it's not a good idea to store age, since age is relative to a person's birthdate. If you have a public function get_age(), you can return a person's age regardless of whether it is stored as an int or a more complex data type, such as date. The caller of get_age() does not need to know how you arrived at the age you returned. If you make age public, and you decide to change it's implementation, you have to change every place in your program that references age. Not a big deal for a small program, but when programs get large, this can be quite tedious.


Last edited on
closed account (3voN6Up4)
Oh... so like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
class Person
{
    double Weight = 314.78;
    double Height = 6.7;
public:
    double get()
    {
        cout << Weight << endl;
        cout << Height << endl;
    }
};
int main()
{
    Person Lucas;
    Lucas.get();
}

Output:
1
2
314.78
6.7


like basically you can't touch anything private outside of the class, right?
Last edited on
so like this?

A couple of problems.

Line 8: The get() function name is a poor choice. This is really a print() function.

Line 11: This was declared a function that returns a double, but you don't return anything. Since this is really a print function, a void type is appropriate.

you can't touch anything private outside of the class, right?

Correct.
closed account (3voN6Up4)
Oh okay, thanks a lot! I get it now :-)
Topic archived. No new replies allowed.