inheritance a new class inherits data & methods of an existing class moreover, the new versions of the class can be extended with new features (new data, new methods & overloaded methods!)
derived class = the new class that inherits data & code from the base class
the key advantage of class inheritance in a program is reuse of existing data and code, either to:
modified the behaviour of a class
to leave the base class unchanged but to create a new variant of it thus preserving the behaviour of the base class, and not effecting other parts of the code that use the base class
the protected & public members of the base class become private members in the derived class
when the access specifier is public:
the protected members of the base class become protected in the derived class, and public members in the base class are public members in the derived class
Implementation - Constructors in the base and derived classes
implementation of a derived class is as for any class
however, declaring an instance of a derived class:
calls a constructor for the base class
then calls the appropriate constructor for the derived class
to control how the instances are constructed, you may specify which base class constructor to call using the following format when implementing the derived class:
(
) : (
) { ... }
Time Example:
the base class:
the derived class (using public inheritance):
//##################### timeday.h
#ifndef TIMEDAY_H
#define TIMEDAY_H #include class Time {
private:
int hours, minutes, seconds;
public:
Time(int hr, int min, int sec);
void display();
}; #endif
//##################### timeday.cpp
#include "timeday.h" Time::Time(int hr, int min, int sec) {