Inheritance Basics
Syntax
class Account { ... }; // base (parent) class
class SavingsAccount : public Account { ... }; // derived (child) class
The derived class inherits the base's members and can add new ones.
"is-a" relationship
Use inheritance when the derived type is a kind of the base type:
- ✅
SavingsAccountis anAccount - ✅
Circleis aShape - ❌
Caris not anEngine(a car has an engine → composition)
Access in derived classes
| Base member | Accessible in derived? | Accessible outside? (public inheritance) |
|---|---|---|
public | ✅ | ✅ |
protected | ✅ | ❌ |
private | ❌ (exists, but hidden) | ❌ |
Inheritance modes
| Mode | Base public becomes | Base protected becomes |
|---|---|---|
public ✅ (almost always) | public | protected |
protected | protected | protected |
private (default for class) | private | private |
| Use public inheritance for "is-a". Private inheritance means "implemented-in-terms-of" (rare). |
Calling base constructors
SavingsAccount(std::string owner, double balance, double rate)
: Account(std::move(owner), balance), rate_(rate) {}
The base part is constructed first.
Calling base functions
Account::print(); inside the derived class calls the base version.
Using base constructors directly
using Account::Account; inherits all base constructors (C++11).
Upcasting
A derived object can be used where a base is expected:
void show(const Account& a);
SavingsAccount s(...);
show(s); // ✅ s is an Account
Protected data — use carefully
Protected data members let derived classes break the base's invariants. Prefer private data + protected/public functions.
Example
#include <iostream>
#include <string>
class Account {
public:
Account(std::string owner, double balance) : owner_(std::move(owner)), balance_(balance) {}
void deposit(double amount) { if (amount > 0) balance_ += amount; }
double balance() const { return balance_; }
const std::string& owner() const { return owner_; }
void print() const { std::cout << owner_ << ": Rs. " << balance_; }
protected:
void adjust(double delta) { balance_ += delta; } // for derived classes only
private:
std::string owner_;
double balance_;
};
class SavingsAccount : public Account {
public:
SavingsAccount(std::string owner, double balance, double rate)
: Account(std::move(owner), balance), rate_(rate) {} // base constructed first
void add_interest() { adjust(balance() * rate_); } // uses a protected helper
void print() const {
Account::print(); // call the base version
std::cout << " (savings @ " << rate_ * 100 << "%)";
}
private:
double rate_;
};
class StudentAccount : public Account {
public:
using Account::Account; // inherit constructors
bool can_overdraft() const { return false; }
};
void statement(const Account& a) { // accepts any Account
std::cout << "Statement for " << a.owner() << ": " << a.balance() << '\n';
}
int main() {
SavingsAccount s("Asha", 10000, 0.05);
s.deposit(2000); // inherited from Account
s.add_interest(); // added by SavingsAccount
s.print();
std::cout << '\n';
StudentAccount st("Ravi", 500);
st.print();
std::cout << ", overdraft? " << std::boolalpha << st.can_overdraft() << '\n';
statement(s); // a SavingsAccount IS an Account
statement(st);
// s.adjust(1000000); // error: adjust is protected
return 0;
}
/* Output:
Asha: Rs. 12600 (savings @ 5%)
Ravi: Rs. 500, overdraft? false
Statement for Asha: 12600
Statement for Ravi: 500
*/