Class Inheritance

Unlike structures, classes support inheritance. For example:

Slice
class TimeOfDay {
    short hour;         // 0 - 23
    short minute;       // 0 - 59
    short second;       // 0 - 59
};

class DateTime extends TimeOfDay {
    short day;          // 1 - 31
    short month;        // 1 - 12
    short year;         // 1753 onwards
};

This example illustrates one major reason for using a class: a class can be extended by inheritance, whereas a structure is not extensible. The previous example defines DateTime to extend the TimeOfDay class with a date.

If you are puzzled by the comment about the year 1753, search the Web for "1752 date change". The intricacies of calendars for various countries prior to that year can keep you occupied for months...

Classes only support single inheritance. The following is illegal:

Slice
class TimeOfDay {
    short hour;         // 0 - 23
    short minute;       // 0 - 59
    short second;       // 0 - 59
};

class Date {
    short day;
    short month;
    short year;
};

class DateTime extends TimeOfDay, Date {   // Error!
    // ...
};

A derived class also cannot redefine a data member of its base class:

Slice
class Base {
    int integer;
};

class Derived extends Base {
    int integer;                // Error, integer redefined
};
See Also