Inheritance

Relationships

Inheritance models an is-a relationship.
- E.g., a checking account is a kind of account.

Composition or aggregation models a has-a relationship.
- E.g., a grade list that has a vector of grades as part of it.

Inheritance Hierarchy

We can have multi level inheritance and have a derived class that inherits from multiple base class.

Declaration and Terminology

class BaseClass {
	// definitions
};
class DerivedClass: public BaseClass {
	// definitions
}

Inheritance and Constructor

Derived classes don't inherit constructors, but usually needs to call their base class constructor to initialize inherited data member.

CheckingAccount(double initial, double atm) :
	Account(initial), total_fees(0.0), atm_fee(atm) { }

Inheritance and Destructor

When the lifetime of a derived object is about to end, two destructors are called: the one defined for the derived object, and the one defined for the base class.