[ANOTHER DRAFT]How To: Private Access in Classes

This article assumes that you know what a class is, and how to declare one, as well as how to create an object of said class.

However, something that usually confuses some beginners is how to access private class variables and functions. One way is through a public member function:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
class mycl
{
private: //note this isn't needed; parts of a class would be private by default
int a_variable, another_one;
public:
int addition(int, int);
};
int mycl::addition(int x, int y){
int z;
a_variable = x;
another_one = y;
z = a_variable + another_one;
return z;
}

int main(){
mycl obj; //creates an object called "obj" which allows access to the addition function
cout << obj.addition(5, 4) << endl;
cin.get();
return 0;
}

Now, the private member functions are used.

If any other ways present themselves to access private access variables or functions, please add them to this article.
Last edited on
The associated common question is 'Why bother with private members and methods?'

As a newcomer to OO private members and methods may appear as just a way to stop you doing things - which they are, but the idea is to produce code where what it does is separated from how it does it.

By making only the parts of the class that are designed to be used by other code public, and the remainder private, you are helping to make that distinction.

By only providing public methods that read a private member you make it 'read only' to external code, for example.

The public / private distinction may seem unimportant when just writing small pices of code for yourself, but are essential when working on larger projects with multiple developers - get into the habit from the start, and it will be easy when you need it!
'Why bother with private members and methods?'


Asked by me as a beginner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

class TheClass
{
      public:
           TheClass();  //The Constructor
           void AddOne();
           void Display();
           void Reset();
      private:
           int number;
}TheObject;

TheClass::TheClass()
{
      number = 0;
}

void TheClass::AddOne()
{
      number++;
}

void TheClass::Display()
{
      cout << "Number is " << number << endl;
}

void TheClass::Reset()
{
      char input;
      cout << "Reset to zero? (y/n)" << endl;
      cin >> input;
      if(input == 'y' || input == 'Y')
            number = 0;
}

int main()
{
      while(1)
      {
             TheObject.Display();
             TheObject.AddOne();
             TheObject.Reset();
      }
      return 0;
}


This shows that private members are only accessed by ways you want it to. Theres no need for TheObject.number to be public because anything I would want to do to is defined as a public method.
Now, the private member functions are used.

member function? It's called a Method.

Why bother with private members and methods?

To prevent the members/methods from being inherited by any potential child classes.

Why bother with protected or private members and methods for access control?

To prevent other programmers who are not familiar with the code from making changes through Class.Member = X. If they assign a value to it that causes unexpected behaviour it's very difficult for you to try down this code and fix the problem.

This is why encapsulation was developed. By using get/set methods you are able to put validation into the functions to ensure values assign will not cause unexpected behavior (and fall within application limits)

By making only the parts of the class that are designed to be used by other code public, and the remainder private, you are helping to make that distinction.


People often go straight to private. But you really need to consider (and will be in your design) that in a good OO application there will be quite a large child-parent hierarchy. You will end up using protected more than you would private.
Encapsulation is also handy because the underlying member variable may change. For example, imagine in the above example the variable "number" is changed from being an intrinsic type to a class type Integer. Or perhaps a Property Bag class which allows you to store the value to disk for later retrieval. By encapsulating, the client code does not have to change. That helps down the road with maintainability.
Topic archived. No new replies allowed.