Previously we looked at a class Circle, which had a property radius that was publicly available. That means that any other class, or your main program could change that value. This is a problem for two reasons:
First, that data could be tampered with. Another function, class, etc could directly access the variable and change it.
Second, it makes your code harder to maintain. You may find a better method of storing data, and/or you want to perform data checks on your data. But if you have public data fields, some programmers can be sloppy, directly access it, and give you bad info.
What type of bad info – well let’s look at our circle class. A field such as radius can be assumed to be positive. You can’t have a negative radius – but a person could send it a value of -1.
Encapsulation is the process of protecting the data, by hiding it, and making it accessible only through other methods. These are often called getters and setters.
The first step in encapsulation is to make the data fields private. When you define their access as private, only that Class has access to them. Think of information about yourself. Some you share with everyone – like your name. Other’s you keep to yourself, and only handout to people you trust – like your credit card number.
When you make your data private, you create a method which you can use to control what information is seen or not, updated, or not.
class Circle {
public:
Circle();
Circle(double newRaidus);
double getArea();
double getDiameter();
private:
double radius;
};
In our header file we make this change. We add a private:
label, and everything after it is private.
Getters
Next we’ll need to create a method to allow someone to access the now private data. This is generally from a method called a getter, since it is usually called get<variable name>().
The return type of the method is usually the data type of the variable stored internally.
If your property is a boolean variable, this method name is often changed to be is<Variable name>.
In your header file you would have:
class Circle {
public:
Circle();
Circle(double newRaidus);
double getArea();
double getDiameter();
double getRadius();
private:
double radius;
};
Then, in your class cpp file, you’d add the following method:
double Circle::getRadius()
{
return radius;
}
This is a simple example where no calculations need to occur and no checks to see if you should have access to the data. In all honesty, this is how most getters are going to work.
Setters
Setter methods get their name because they usually start with set, and are called set<Variable Name>(). They usually are going to take a parameter of the same type as the variable, but not necessarily.
While getters are fairly simple, setters can be fairly complex, running calculations, condition statements, etc, based upon the need and the data being stored.
For our example, let’s loot at the setRadius() method.
In the header file we need to add the new method prototype.
class Circle {
public:
Circle();
Circle(double);
double getArea();
double getDiameter();
double getRadius();
void setRadius(double);
private:
double radius;
};
In the class CPP file, we’d add the following:
void Circle::setRadius(double newRadius)
{
radius = newRadius;
}
This however is an overly simplified example. Radius could be accidentally set to a negative number. So we need to test this:
void Circle::setRadius(double newRadius)
{
if( newRadius > 0 )
{
radius = newRadius;
}
}
Here we make sure we get a positive number. Zero, or anything less, will essentially be ignored.
We might also allow for setters to be overloaded. For example, what happens if a string is sent? Right now, and error occurs. But if we overload it, and convert it to a number, then we could handle a different datatype.
Variable Scope
When talking able encapsulation, it’s a good time to talk about variable scope when it comes to classes. Scope envelopes not only where it can be seen, but who can access it.
I’ve already mentioned that class variables within a class are only accessible to that specific object. Other objects cannot access those variables.
Likewise, if you set a variable’s accessor property to be private, no one can access that property. Only that object.
Class level variables remain in memory and are accessible to the object that created them, as long as the object is still around. So create it at the beginning of the program, and it is accessible until the program ends. Create it within a function call, and when the function call ends, so does the object, and thus the class level variable.
If you have a method in a class, and you create a variable inside that method. It is only accessible to that method, and it’s data is lost once the method exits, just like with a regular function.
Data Field Encapsulation was originally found on Access 2 Learn