C++

Encapsulation

Encapsulation is a concept that bundles data variables and the functions that manipulate them into a single unit called a class, while simultaneously restricting direct external access to that data. It acts as a protective shield (like a medical capsule) to prevent unauthorized modifications and preserve data integrity

Encapsulation is to make sure that "sensitive" data is hidden from users.

To achieve this, class variables/attributes are declared as private (cannot be accessed from outside the class).

If others are to read or modify the value of a private member, public set and get methods are provided to assign and access values respectively.

We will modify our Paddle class in the constructor lessons to protect the X and Y coordinate variables in the class. We will use set and get methods to assign and access these values.

When an object is created and a variable or method is accessed as objectName.variableName or objectName.method() respectively, a private access error occurs. Variables can be assigned values using the set methods and accessed using the get methods

#include <iostream>
using namespace std;
class Paddle{
    private:
    int xPos;
    int yPos;
    int interval = 5;
    public:
    Paddle(int x, int y){
        xPos = x;
        yPos = y;
    }
    void moveUp(){
        yPos = yPos + interval;
    }
    void moveDown(){
        yPos = yPos - interval;
    }
    void setXPosition(int x){
        xPos = x;
    }
    void setYPosition(int y){
        yPos = y;
    }
    int getXPosition(){
        return xPos;
    }
    int getYPosition(){
        return yPos;
    }
};
int main() {
    Paddle paddle(5,50);
    //Show the x and y coordinates of the paddle
    cout << "Paddle is at (" << paddle.getXPosition() << "," << paddle.getYPosition() << ")" << endl;
    //The up arrow key pressed 5 times
    paddle.moveUp();
    paddle.moveUp();
    paddle.moveUp();
    paddle.moveUp();
    paddle.moveUp();
    cout << "Paddle is now at (" << paddle.getXPosition() << "," << paddle.getYPosition() << ")" << endl;
    //The down arrow pressed 2 times
    paddle.moveDown();
    paddle.moveDown();
    cout << "Paddle is now at (" << paddle.getXPosition() << "," << paddle.getYPosition() << ")" << endl;
    //Set private values using set methods
    paddle.setXPosition(50);
    paddle.setYPosition(20);
    cout << "Paddle is now at (" << paddle.getXPosition() << "," << paddle.getYPosition() << ")" << endl;
     
    return 0;
}

Notes:

  1. Assigning values to xPos and yPos as paddle.xPos = 5, paddle.yPos = 50, will produce private access error
  2. The constructor method is specified as public in order to use the constructor to initialize the private variables when an object is created
  3. setXPosition method is used to assign new value to the xPos private variable
  4. setYPosition method is used to assign new value to the yPos private variable
  5. getXPosition and getYPosition methods are used to get the private xPos and yPos variable values respectively
  6. The xPos and yPos variables are therefore encapsulated (hidden from objects created)