C++
Beginner
1 min read
Encapsulation and Access Specifiers
Example
#include <iostream>
#include <string>
#include <stdexcept>
class BankAccount {
public:
// Constructor — public interface for creation
explicit BankAccount(const std::string& owner, double initial = 0.0)
: owner_(owner), balance_(initial) {
if (initial < 0) throw std::invalid_argument("Negative balance");
}
// Public accessors (getters)
std::string owner() const { return owner_; }
double balance() const { return balance_; }
// Public mutators with validation
void deposit(double amount) {
if (amount <= 0) throw std::invalid_argument("Amount must be positive");
balance_ += amount;
}
void withdraw(double amount) {
if (amount <= 0) throw std::invalid_argument("Amount must be positive");
if (amount > balance_) throw std::runtime_error("Insufficient funds");
balance_ -= amount;
}
private:
std::string owner_; // private data
double balance_; // private data
// Private helper — not part of public API
void log(const std::string& msg) const {
std::cout << "[" << owner_ << "] " << msg << "\n";
}
};
int main() {
BankAccount acc("Alice", 1000.0);
acc.deposit(250.0);
acc.withdraw(100.0);
std::cout << acc.owner() << " balance: " << acc.balance() << "\n";
return 0;
}