Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin
Znav
nextClass Inheritance Semantics
prevSimple Classes

Unlike structures, classes support inheritance. For example:

Wiki Markup
{zcode: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
};
{zcode}

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.

Info

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:

Wiki Markup
{zcode: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!
    // ...
};
{zcode}

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

Wiki Markup
{zcode:slice}
class Base {
    int integer;
};

class Derived extends Base {
    int integer;                // Error, integer redefined
};
{zcode}
Ztop
See Also
Zret
Znav
nextClass Inheritance Semantics
prevSimple Classes