Team 2550
Technical Documentation
 All Files Variables Pages
Encapsulation

The protection of certain members of a class is called encapsulation.

  • Often, you want the user of the class to be able to read the contents of a variable but not change it directly.
  • Some methods may be implementation details, useful for writing the class but not useful for using it.
  • In many situations, you need to check a value before writing it to a variable (and notify the user if it is invalid). A setter method allows you to do this.

You have already seen some encapsulation in action. Remember the public and private designations in the Vector2d class? Those are the primary means of encapsulation.

  • private: The user does not have access to the data, they must interact with that data via methods.
  • public: The user has full access to both read and change the variable.
Note
For future reference: "the user" is a programmer using the class you wrote. This is different from the "end user" (a.k.a. the consumer).

Typically, you want to restrict direct variable access as much as possible. If encapsulation is not necessary, it is typically best to use a struct. Let's compare a struct and class implementation of a type called ColorRGB...

Problem statement: Implement an RGB color class or struct that will be fed into a multicolor LED light. The output on the computer can send unique sinals between 0 and 1023. Invalid output could damage the hardware.

class struct
Can restrict access to the RGB values and make sure they are not out of bounds. Can only store the information, the user is responsible for validating the data.
Due to indirect access, methods can sometimes be slower than changing the values manually. You don't always have to validate data. You only really need to validate the data when it is coming from an input with different bounds.
Methods are very convenient and fast. The equivalent of methods can be achieved by passing the struct into a function by reference, but this can be slower and is much less convenient.
Default values can be initialized in the constructor. There is no way to initialize the values besides setting each one individually. A constructor-like function can be created that returns an instance of the struct with default values set, but you have to run it manually.
There can be a send method that outputs the color to the light. The new color could even be sent when the set method is run. In this situation, a color is the only thing that will be output.
In order to support multiple default values, there will have to be an additional value within the class that states the maximum value for any color. If you have an array with, say, 1000 objects, it will take a substantial amount of space. Because the user will validate the value themselves, there is no need for the extra variable.
Note
In C++, the only technical difference between a struct and class is their default encapsulation level. Declarations within a struct not under encapsulation are automtically public, while a class makes them private. Although it is possible, it is generally not a good idea to place methods in a struct.

For this particular application, it is difficult to know which to use. I would probably use a class in this case.

  • The likeliness of human error is fairly high. By validating the values, I will be guaranteed that the LED will not be damaged.
  • The user does not need to manually validate the values, meaning that they can focus more on the logic of the program.
  • The class is more intuitive, implementing a send() method could be very useful, and it will be easier to read later on.
  • For any given project, the range of values for a single LED will not change, which omits the extra variable.
Note
This is not always the case, and you will see many situations in which a struct is better than a class in the future.

As a rule of thumb, variables are usually private and methods are public.

Example

class DataType {
private:
int x[1000];
double y;
public:
DataType();
void addToArray(int data);
int getFromArray();
double gety();
};
//The order of public and private does not matter.

In C++, there is no technical difference between a struct and class. There is only one slight behavioral difference. In a class, data not under an encapsulation label is automatically made private, whereas in a struct it is public.

Warning
It is not reccomended to put methods in a struct, the struct keyword came from C, which does not support methods.