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
}
-
Derived class inherits from base class.
- In Java-like vocab: "subclass inherits from superclass".
-
This is a public inheritance (most common)
- Access of members in the base class is passed down and preserved.
-
protected&privateinheritance is possible (not common). If you forget to explicitly saypublic, the default isprivateprotectedinheritance means you will inherit all thepublicandprotectedmembers asprotectedfield. That means all thepublicmember fields/functions in the base class, after your inheritance in the derived class, will becomeprotectedmember fields/functions. This means that future classes that derive from this derived class will not be able to access it.privateinheritance is even stronger. Allpublicorprotectedfields/functions will becomeprivatein the derived class. Any class that tries to inherit this class won't have any access right to the protected fields of your base class (since the first inheritance changed the access right fromprotectedtoprivate.)- Basically, the access right defined here will determine what access right the public inherited fields in the base class will have.
-
Derived class inherits most members of base class, regardless of protection. They can only access
publicandprotected -
Derived class does not inherit
- constructors
- assignment operators if explicitly defined
-
Derived class cannot delete things it inherited; cannot pick and choose what to inherit
-
Derived class can override inherited member functions.
Inheritance and Constructor
Derived classes don't inherit constructors, but usually needs to call their base class constructor to initialize inherited data member.
- Do this with the base class name in C++ (no
super()like in Java). - The base class constructor must be the first thing in the derived class constructor because if it is missing, then a default constructor for the base class will be called automatically; error if one doesn't exist.
CheckingAccount(double initial, double atm) :
Account(initial), total_fees(0.0), atm_fee(atm) { }
- When a derived class object is created, its inherited (base) parts must be initialized before any newly defined parts by executing a base constructor
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.
- Destructor may be explicitly defined, or just provided the default
- A chain call happens for multi-level inheritance
- Constructors and destructors are executed in opposite orders!