Object-Oriented Programming (OOP)
C++ provides classes and virtual functions directly; C can model similar relationships with structs, controlled functions, and function pointers.
What is object-oriented programming in C++?
OOP organizes a program around objects with state, invariants, and behavior. C++ supports this model directly with classes, access control, inheritance, and virtual dispatch; C can construct a similar design explicitly.
The four core OOP principles
- Encapsulation: a class protects its invariants by controlling access to internal state.
- Abstraction: callers depend on a small public interface instead of storage and implementation details.
- Inheritance: a derived class extends or specializes a valid base-class contract.
- Polymorphism: a virtual call selects behavior from the concrete object at runtime.
How C and C++ express the design
C keeps data in a struct and stores behavior in function pointers. Conventions and API functions must enforce invariants, ownership, and valid dispatch.
C++ keeps balance private, inherits a common Account interface, and overrides a virtual operation. The compiler checks access and override signatures.
C and C++ code examples
#include <stdio.h>
typedef struct Account Account;
typedef void (*MonthEnd)(Account *account);
struct Account {
const char *owner;
double balance;
double interest_rate;
MonthEnd month_end;
};
void standard_month_end(Account *account) {
(void)account;
}
void savings_month_end(Account *account) {
account->balance *= 1.0 + account->interest_rate;
}
void print_account(Account *account) {
account->month_end(account);
printf("%s: %.2f\n", account->owner, account->balance);
}
int main(void) {
Account accounts[] = {
{"Ada", 150.0, 0.0, standard_month_end},
{"Lin", 200.0, 0.05, savings_month_end}
};
for (int i = 0; i < 2; ++i) {
print_account(&accounts[i]);
}
return 0;
}
Ada: 150.00
Lin: 210.00
#include <iomanip>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
class Account {
std::string owner_;
double balance_;
protected:
void add_balance(double amount) { balance_ += amount; }
public:
Account(std::string owner, double balance)
: owner_(std::move(owner)), balance_(balance) {}
virtual ~Account() = default;
virtual void month_end() {}
const std::string& owner() const { return owner_; }
double balance() const { return balance_; }
};
class SavingsAccount final : public Account {
double interest_rate_;
public:
SavingsAccount(std::string owner, double balance, double rate)
: Account(std::move(owner), balance), interest_rate_(rate) {}
void month_end() override {
add_balance(balance() * interest_rate_);
}
};
int main() {
Account standard("Ada", 150.0);
SavingsAccount savings("Lin", 200.0, 0.05);
std::vector<Account *> accounts{&standard, &savings};
std::cout << std::fixed << std::setprecision(2);
for (Account *account : accounts) {
account->month_end();
std::cout << account->owner() << ": "
<< account->balance() << '\n';
}
}
Ada: 150.00
Lin: 210.00
C and C++ comparison
The C version stores state in a struct and selects month-end behavior through a function pointer. The C++ version keeps balance private, inherits a common interface, and uses a virtual override. In both versions, client code invokes one operation without branching on the concrete account type.
C uses naming conventions, callbacks, macros, and explicit context structures.
Language features provide scoped, type-safe, and often zero-overhead abstractions.
Practice exercises
Run both versions, then modify them to observe the different language guarantees.
- Write the C mechanism before the C++ abstraction.
- Remove manual cleanup with RAII.
- Check whether the abstraction adds allocations or virtual dispatch.