Besides being unable to directly access private members of a parent class, several parent class members are never inherited:
constructors
destructors
friend functions
overloaded new operators
overloaded = operators
NOTE: If a derived class requires any of the preceding items, the item must be explicitly defined within the derived class definition.
We described various UML relationships and worked our way through most of them.
The last on the list was generalisation.
Generalisation is a form of software reusability realized, or supported, through inheritance.
New classes are created from existing classes by:
Absorbing their attributes and behaviours.
Adding, changing, or replacing some of the behaviours with capabilities the derived class requires.
Inheritance models an “is a” type of relationship between objects:
An object of a derived class type may also be treated as an object of the base type.
There are various advantages:
A substantial part of the code is already written.
You can extend a base class without duplicating the existing base class properties.
Existing code has already been tested, so should be reliable.
Since you already understand how the base class works, you can concentrate on writing the extensions.
Due to the “is a” relation we can have collections of multiple related types.
A derived class inherits attributes and methods from its base class. The derived class adds new properties to those inherited from the base class. Parent classes tend to be more abstract than derived classes, which are more specific...
OOM implies a top-down approach for software system design (the class model)
More general ( more abstract ) system components are not dependent upon more detailed ( more specific ) components
If we need to write a program using a new class named SeniorStaff, it may be easier if SeniorStaff could reuse properties of an already defined class Staff.
SeniorStaff will not need to redefine members which are already defined as Staff members.
SeniorStaff may also require some additional data members and functions, such as bonus or getBonus().
The SeniorStaff class might require a different display format than the Staff class, so the display() function may need to be replaced with a tailored version.
SeniorStaff inherits from Staff, or is derived from it.
Staff is called a parent class, base class, superclass, or ancestor.
SeniorStaff is called a child class, derived class, subclass, or descendant.
To build on properties inherited from a parent class:
We define new data members in the derived class.
To build on behaviours inherited from a parent class:
Define new member functions in the derived class.
Substitute functions defined in the parent class for others in the derived class.
Object-oriented programmers say that inheritance supports generalisation.
class Staff {
private:
int idNum;
string firstName;
string lastName;
public:
Staff();
~Staff();
void display();
int getId();
};
class SeniorStaff : public Staff {
private:
float bonus;
public:
SeniorStaff();
~SeniorStaff();
void display();
float getBonus();
void addBonus(float);
};