C++ and Object-Oriented Programming

OOP Concepts — Overview; Encapsulation; Abstraction

C-CAT

OOP Concepts — Overview

The Four Pillars of OOP

+----------------------------------------------+
|         Object-Oriented Programming          |
+----------+----------+----------+-------------+
|Encaps-   |Abstraction|Inheritance|Polymorphism|
|ulation   |           |           |            |
+----------+----------+----------+-------------+

Object State, Behavior, Identity

Every object has three characteristics:

CharacteristicDefinitionExample (Car)
StateValues stored in data membersColor=Red, Speed=60km/h, Fuel=50L
BehaviorWhat an object does (its methods)accelerate(), brake(), turn()
IdentityUnique identifier distinguishing objectsVIN number (each car unique)
class Car {
private:
    // STATE (data members)
    string color;
    int speed;
    float fuel;

public:
    // BEHAVIOR (member functions)
    void accelerate(int by) { speed += by; }
    void brake() { if (speed > 0) speed -= 10; }
    void display() {
        cout << "Color: " << color << ", Speed: " << speed << endl;
    }
    // IDENTITY: each Car object has unique address in memory
};

Encapsulation

What is Encapsulation?

Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit (class), while hiding the implementation details.

Benefits:

  • Data protection — external code cannot directly modify data
  • Implementation can change without affecting users
  • Better maintainability and readability

Access Specifiers

SpecifierAccessible from
privateOnly within the class (default for class)
protectedWithin class + derived classes
publicAnywhere (from main, other classes)
class BankAccount {
private:
    // Hidden from outside - ENCAPSULATED
    double balance;
    string account_number;
    string password;

public:
    // Controlled access through public methods
    BankAccount(string acc_no, string pwd, double initial_balance) {
        account_number = acc_no;
        password = pwd;
        balance = initial_balance;
    }

    bool deposit(double amount) {
        if (amount <= 0) return false;
        balance += amount;
        return true;
    }

    bool withdraw(double amount, string pwd) {
        if (pwd != password) {
            cout << "Wrong password!\n";
            return false;
        }
        if (amount > balance) {
            cout << "Insufficient funds!\n";
            return false;
        }
        balance -= amount;
        return true;
    }

    double get_balance(string pwd) {
        if (pwd != password) {
            cout << "Wrong password!\n";
            return -1;
        }
        return balance;
    }
};

Getters and Setters

class Employee {
private:
    int id;
    string name;
    double salary;  // private - cannot be negative

public:
    // Getter
    double get_salary() const { return salary; }
    string get_name() const { return name; }

    // Setter with validation
    bool set_salary(double s) {
        if (s < 0) return false;   // validate!
        salary = s;
        return true;
    }

    void set_name(string n) {
        if (!n.empty()) name = n;
    }
};

Abstraction

What is Abstraction?

Abstraction is showing only the essential features (interface) of an object while hiding the complex implementation details.

ATM Machine Example:
  What you see (Interface):   PIN pad, card slot, cash tray, buttons
  What's hidden (Impl):       Banking protocol, database, security, cash counting

Abstraction vs Encapsulation:

ConceptFocusHow
AbstractionHiding complexity; showing essentialInterface (abstract class, pure virtual)
EncapsulationHiding internal state; controlled accessAccess specifiers (private/protected)

Continue learning

Related notes

Put this topic into timed practice

Open mock tests when you want full-exam pacing, or keep drilling in practice mode.