Encapsulation - heshawacooray/OOP-Heshawa GitHub Wiki

Definition: Encapsulation is the principle of hiding the internal state of an object and only allowing access or modification through defined methods (getters and setters). It helps protect object data from unintended interference and misuse.

Example code:

#include <iostream>
using namespace std;

class Car {
private:
    string brand;
    int year;

public:
    void setBrand(string b) {
        brand = b;
    }

    string getBrand() {
        return brand;
    }

    void display() {
        cout << "Brand: " << brand << ", Year: " << year << endl;
    }
};

int main() {
    Car car1;
    car1.setBrand("Toyota");  
    cout << "Brand: " << car1.getBrand() << endl;

    return 0;
}
⚠️ **GitHub.com Fallback** ⚠️