Wrox Press C++ Tutorial


Data Types, Objects, Classes and Instances

So far, we've learnt that C++ lets you create variables which can be any of a range of basic data types: int, long, double and so on. However, the variables of the basic types don't allow you to model real-world objects (or even imaginary objects) adequately. It's hard to model a box in terms of an int, for example; what we need is something that allows us to collect together the various attributes of an object. In C++, we can do this very easily using classes.

You could define variables, length, breadth and height to represent the dimensions of the box and bind them together as members of a Box class, as follows:

class Box
{
public:
   double length;
   double breadth;
   double height;
};

This code actually creates a new type, called Box. The keyword class defines Box as such, and the elements that make up this class are defined within the curly braces. Note that each line defining an element of the class is terminated by a semicolon, and that a semicolon also appears after the closing brace. The elements length, breadth and height are referred to as data members. At the top of the class definition, you can see we have put the keyword public - this just means that the data members are generally accessible from outside the class. You may, however, place restrictions on the accessibility of class members, and we'll see how this works a bit later in this chapter.

With this definition of a new data type called Box, you can go ahead and define variables of this type just as you did with variables of the basic types:

Box myBox;              //Declare a variable myBox of type Box

Once we've defined the class Box, the declaration of variables of this type is quite standard. The variable myBox here is also referred to as an object or an instance of the class Box.

With this definition of a new data type called Box, you can go ahead and define variables of this type just as you did with variables of the basic types. You can then create, manipulate and destroy as many Box objects as you need to in your program. This means that you can model objects using classes and write your programs around them. So - that's object-oriented programming all wrapped up then?

Well, not quite. You see, object-oriented programming (OOP) is based on a number of foundations (famously encapsulation, polymorphism and inheritance) and what we have seen so far doesn't quite fit the bill. Don't worry about what these terms mean for the moment - we'll be exploring these ideas in more detail as we learn more about what you can do with classes.

First Class

The notion of class was invented by an Englishman to keep the general population happy. It derives from the theory that people who knew their place and function in society would be much more secure and comfortable in life than those who did not. The famous Dane, Bjarne Stroustrup, who invented C++, undoubtedly acquired a deep knowledge of class concepts while at Cambridge University in England, and appropriated the idea very successfully for use in his new language.

The idea of a class in C++ is similar to the English concept, in that each class usually has a very precise role and a permitted set of actions. However, it differs from the English idea, because class in C++ has largely socialist overtones, concentrating on the importance of working classes. Indeed, in some ways it is the reverse of the English ideal, because, as we shall see, working classes in C++ often live on the backs of classes that do nothing at all.

Operations on Classes

In C++ you can create new data types as classes to represent whatever kinds of objects you like. As you'll come to see, classes aren't limited to just holding data; you can also define member functions that act on your objects, or even operations that act between objects of your classes using the standard C++ operators. You can define the class Box, for example, so that the following statements work and have the meanings you want them to have:

Box Box1;
Box Box2;
if(Box1 > Box2)       // Fill the larger box
   Box1.Fill();
else
   Box2.Fill();

You could also implement operations as part of the Box class for adding, subtracting or even multiplying boxes - in fact, almost any operation to which you could ascribe a sensible meaning in the context of boxes.

We're talking about incredibly powerful medicine here and it constitutes a major change in the approach that we can take to programming. Instead of breaking down a problem in terms of what are essentially computer-related data types (integer numbers, floating point numbers and so on) and then writing a program, we're going to be programming in terms of problem-related data types, in other words classes. These classes might be named Employee, or Cowboy, or Cheese or Chutney, each defined specifically for the kind of problem that you want to solve, complete with the functions and operators that are necessary to manipulate instances of your new types.

Program design now starts with deciding what new application-specific data types you need to solve the problem in hand and writing the program in terms of operations on the specifics that the problem is concerned with, be it Coffins or Cowpokes.

Terminology

Let's summarize some of the terminology that we will be using when discussing classes in C++:

When we get into the detail of object-oriented programming, it may seem a little complicated in places, but getting back to the basics of what you're doing can often help to make things clearer, so always keep in mind what objects are really about. They are about writing programs in terms of the objects that are specific to the domain of your problem. All the facilities around classes in C++ are there to make this as comprehensive and flexible as possible. Let's get down to the business of understanding classes.


© 1998 Wrox Press