Members, Access Specifiers & Encapsulation
Access specifiers
| Specifier | Accessible from |
|---|---|
public | anywhere |
private | only member functions (and friends) of the same class |
protected | the class and derived classes (Unit 9) |
class Student {
public: // the interface
void set_marks(int m);
int get_marks() const;
private: // the implementation
int marks = 0;
};
Typical layout: public first (what users need), then private.
Getters and setters
int get_marks() const { return marks; }
void set_marks(int m) {
if (m < 0 || m > 100) throw std::invalid_argument("marks must be 0-100");
marks = m;
}
Setters can validate, log, or update related data. A class with getters and setters for every field and no rules is just a struct with extra typing — only add what's needed.
Default member initializers (C++11)
private:
int marks = 0;
std::string name = "Unknown";
Invariants
An invariant is a rule always true for valid objects:
- balance ≥ 0
- month between 1 and 12
size <= capacityEncapsulation guarantees invariants because only member functions can change the data.
Access is per class, not per object
A member function can access private members of another object of the same class (useful for comparisons and copying).
Benefits
- ✅ Change the internal representation (e.g. store paisa instead of rupees) without breaking users
- ✅ Validation in one place
- ✅ Easier debugging: only a few functions can change the data
Naming private members
Common styles: balance_, m_balance, or just balance. Be consistent.
Example
#include <iostream>
#include <stdexcept>
#include <string>
class Student {
public:
void set_name(const std::string& n) { name_ = n.empty() ? "Unknown" : n; }
void set_marks(int m) {
if (m < 0 || m > 100) throw std::invalid_argument("marks must be between 0 and 100");
marks_ = m;
}
const std::string& name() const { return name_; }
int marks() const { return marks_; }
char grade() const { return marks_ >= 80 ? 'A' : marks_ >= 60 ? 'B' : marks_ >= 40 ? 'C' : 'F'; }
bool scored_higher_than(const Student& other) const {
return marks_ > other.marks_; // same class: may read other's private data
}
private:
std::string name_ = "Unknown";
int marks_ = 0;
};
class Money { // the representation is hidden: stored in paisa
public:
void set_rupees(double r) { paisa_ = static_cast<long long>(r * 100 + 0.5); }
double rupees() const { return paisa_ / 100.0; }
void add(const Money& other) { paisa_ += other.paisa_; }
private:
long long paisa_ = 0;
};
int main() {
Student a, b;
a.set_name("Asha");
a.set_marks(86);
b.set_name("");
b.set_marks(58);
std::cout << a.name() << ": " << a.marks() << " (" << a.grade() << ")\n";
std::cout << b.name() << ": " << b.marks() << " (" << b.grade() << ")\n";
std::cout << a.name() << " scored higher? " << std::boolalpha << a.scored_higher_than(b) << '\n';
try {
b.set_marks(150);
} catch (const std::invalid_argument& e) {
std::cout << "Rejected: " << e.what() << '\n';
}
std::cout << b.name() << " still has " << b.marks() << '\n';
Money m1, m2;
m1.set_rupees(0.1);
m2.set_rupees(0.2);
m1.add(m2);
std::cout << "0.1 + 0.2 rupees = " << m1.rupees() << " (exact, thanks to integer paisa)\n";
return 0;
}
/* Output:
Asha: 86 (A)
Unknown: 58 (C)
Asha scored higher? true
Rejected: marks must be between 0 and 100
Unknown still has 58
0.1 + 0.2 rupees = 0.3 (exact, thanks to integer paisa)
*/