Encapsulation - Tryhardtocarry/2143-oop GitHub Wiki
Encapsulation is an OOP principle that restricts direct access to certain components of an object.
It is achieved by hiding data using private
and exposing only the necessary parts through public methods.
In video games, encapsulation is used to protect game mechanics by restricting access to internal data, such as a player's health, experience points, or inventory.
#include <iostream>
using namespace std;
class Player {
private:
int health; // Private variable (cannot be accessed directly)
int maxHealth;
public:
// Constructor to initialize player health
Player(int maxHP) {
maxHealth = maxHP;
health = maxHP; // Player starts with full health
}