Wrox Press C++ Tutorial


Basic Ideas of OOP

As we have seen, a class is a data type that you define to suit your own application requirements. Classes in object-oriented programming also define the objects to which your program relates. You program the solution to a problem in terms of the objects that are specific to the problem, using operations that work directly with those objects. You can define a class to represent something abstract, such as a complex number, which is a mathematical concept, or a truck, which is decidedly physical (especially if you run into one on the highway). So, as well as being a data type, a class can also be a definition of a real-world object, at least to the degree necessary to solve a given problem.

You can think of a class as defining the characteristics of a particular group of things that are specified by a common set of parameters and share a common set of operations that may be performed on them. The operations that are possible are defined by the class interface contained in the public section of the class definition. The Box class that we used in the last chapter is a good example - it defined a box in the most elementary terms.

Of course, in the real world there are many different kinds of box: there are cartons, coffins, candy boxes and cereal boxes, and you will certainly be able to come up with many others. You can differentiate boxes by the kinds of things they hold, the materials they are made of, and in a multitude of other ways, but even though there are many different kinds of box, they share some common characteristics - the essence of boxiness perhaps - and, therefore, you can still visualize them as actually being related to one another, even though they have many differentiating features. You could define a particular kind of box as having the generic characteristics of all boxes - perhaps just a length, a breadth and a height - plus some additional parameters which serve to differentiate your kind of box from the rest. You may also find that there are new things you can do with your particular kind of box that you can't do with other boxes.

Equally, some objects may be the result of combining a particular kind of box with some other kind of object: a box of candy, or a crate of beer for example. You can, of course, define one kind of box as a generic box, plus some additional characteristics, and then specify another sort of box as a further specialization of that. An example of the kinds of relationships you might define between different sorts of boxes is illustrated below.

The boxes become more specialized as you move down the diagram. In this case, we have defined three different kinds of box, based on the generic type. We also have beer crates defined as a further refinement of crates designed to hold bottles.

You could deduce from this that a good way to approximate the real world relatively well, using classes in C++, would be through the ability to define classes that are interrelated. A candy box can be considered to be a box with all the characteristics of a basic box, plus a few characteristics of its own. This precisely illustrates the relationship between classes in C++ when one class is defined based on another, and is shown in the diagram above. Let's look at how this works in practice.


© 1998 Wrox Press