Why OOP? struct vs class
The problem OOP solves
With separate variables and functions, any code can do balance = -999999;. As programs grow, keeping data valid gets harder.
OOP groups data (members) and behaviour (member functions/methods) into objects, and controls who can change the data.
The four pillars of OOP
| Pillar | Meaning | C++ tool |
|---|---|---|
| Encapsulation | hide internal data, expose a safe interface | private, public |
| Abstraction | show what an object does, hide how | classes, interfaces |
| Inheritance | build new types from existing ones | class Savings : public Account |
| Polymorphism | one interface, many behaviours | virtual functions |
Class and object
- Class: a blueprint/type (
BankAccount) - Object: an instance of the class (
BankAccount asha;)
struct vs class in C++
They are almost identical. The only difference is the default access:
| Default member access | Default inheritance | |
|---|---|---|
struct | public | public |
class | private | private |
Convention ✅
struct→ simple data bundles with no rules to protect (aPoint, aTransactionrecord)class→ types with invariants (rules that must always be true, like "balance ≥ 0") and private data
Member functions
struct Rectangle {
double width, height;
double area() const { return width * height; }
};
Rectangle r{ 3, 4 };
r.area(); // 12
Member functions can use the object's members directly.
Objects are values
Copying an object copies all its members: Rectangle r2 = r; creates an independent copy.
Example
#include <iostream>
#include <string>
// struct: a plain data record, everything public
struct Transaction {
std::string description;
double amount;
};
// class: protects its data with rules (an invariant: balance never negative)
class Wallet {
public:
bool spend(const Transaction& t) {
if (t.amount <= 0 || t.amount > balance) return false; // the class enforces its rules
balance -= t.amount;
++count;
return true;
}
void top_up(double amount) { if (amount > 0) balance += amount; }
double get_balance() const { return balance; }
int transactions() const { return count; }
private:
double balance = 0; // nobody outside can set this directly
int count = 0;
};
int main() {
Transaction lunch{ "Lunch", 350 };
Transaction phone{ "New phone", 95000 };
lunch.amount = 400; // struct: free access is fine for plain data
Wallet w;
w.top_up(5000);
std::cout << "Lunch paid? " << std::boolalpha << w.spend(lunch) << '\n';
std::cout << "Phone paid? " << w.spend(phone) << '\n';
std::cout << "Negative paid? " << w.spend({ "Hack", -1000 }) << '\n';
// w.balance = 1000000; // error: 'balance' is private
std::cout << "Balance: " << w.get_balance() << " after " << w.transactions() << " transaction(s)\n";
Wallet copy = w; // objects are values: an independent copy
copy.top_up(100);
std::cout << "Original " << w.get_balance() << ", copy " << copy.get_balance() << '\n';
return 0;
}
/* Output:
Lunch paid? true
Phone paid? false
Negative paid? false
Balance: 4600 after 1 transaction(s)
Original 4600, copy 4700
*/