A class defines a new type that extends the type system of the language. In languages like C++, a type is a skeleton for an area of memory containing values that follow a particular format. For instance, an integer may be 32 bits of binary number in the 2s complement format; a float may be 32 bits of binary number in the IEEE floating-point format. However, a type may also be seen as a model for a type of object found in the real world.
Things in the real world are classified into groupings called natural types. Each natural type has a number of attributes, called properties that are possessed by all the objects of that type. For instance, the class of all cats is a type with many properties, such as warm-blooded, four-legged, hairy and whiskered. All cats have these properties, and the presence of these properties distinguishes cats from, for instance, humans, which are warm-blooded and hairy, but only have two legs, and no whiskers. When we represent such classes using C++, we could duplicate the common properties like this:
class human { <warm-blooded> <hairy> <two-legged> };
class cat { <warm-blooded> <hairy> <four-legged> <whiskered> };
However, we can leave the common properties in the human class, and allow the cat class to inherit these properties, avoiding the duplication:
class cat : public human { <four-legged> <whiskered> };
Of course, we should also stop the inheritance of the property four-legged, and we can do that by over-riding a property with another of the same name. See C++ inheritance for the details.
Copyright © 2003 Roger Hartley