TYPES, OBJECTS, CLASSES, AND INSTANCES

Before I get into the language, syntax, and programming techniques for classes, I’ll first explain how your existing knowledge relates to the concept of classes. So far, you’ve learned that C++ lets you create variables that can be any of a range of fundamental data types: int, long, double, and so on. You have also seen how you can use the struct keyword to define a structure type that you can then use as the type for a variable representing a composite of several other variables.

The variables of the fundamental 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; however, you can use the members of a struct to define a set of attributes for such an object. You could define variables length, width, and height to represent the dimensions of the box and bind them together as members of a Box structure, as follows:

struct Box
{
  double length;
  double width;
  double height;
};

With this definition of a new data type called Box, you can 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 structs and write your programs around them.

So — that’s object-oriented programming all wrapped up, then? Well, not quite. Object-oriented programming (OOP) is based on three basic concepts relating ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.