Defining C++ Classes
- Defining a class
- Creating a class instance
- Accessing class members
- Encapsulation
- Constructors and destructors
- Inline member functions
- The this pointer
- Static class members
This Chapter introduces C++ Classes. When you define a class, you create a new data type, which you can use much like on of the built - in C++ data types. A class, however, contains code as well as data. A class allows you to encapsulate all a window on the screen, a figure that the program draws, a device connected to a techniques for creating and using individual classes. In the next chapter, you'll learn how to define and use hierachies of related classes.
- Defining a Class
A C++ class is somewhat similar to a standard C structure, though the features of C++ classes go far beyond those provided by C structures. To understand C++ classes, it's useful to start by considering how structure are used in the C language.
A C structure allows you to group a set of related data items. As a simple example, if your C program draws a rectangle on the screen, you might find it convenient to store the coodinates of the rectangle within a structure defined as follows :
Struct Rectangle
{
int Left;
int Top;
int Right;
int Bottom;
};
You could then define a function for drawing the rectangle, as in the following example.
Void Draw Rectangle (Struct Rectangle *Rect)
You could then define a function for drawing the rectangle, as in the following example.
Void Draw Rectangle (Struct Rectangle *Rect)