Class declarations introduce new types, called class names, into programs. These class declarations also act as definitions of the class for a given translation unit. There may be only one definition for a given class type per translation unit. Using these new class types, you can declare objects, and the compiler can perform type checking to verify that no operations incompatible with the types are performed on the objects.
An example of such type checking is:
class Point
{
public:
unsigned x, y;
};
class Rect
{
public:
unsigned x1, y1, x2, y2;
};
// Prototype a function that takes two arguments, one of type
// Point and the other of type pointer to Rect.
int PtInRect( Point, Rect & );
...
Point pt;
Rect rect;
rect = pt; // Error. Types are incompatible.
pt = rect; // Error. Types are incompatible.
// Error. Arguments to PtInRect are reversed.
cout << “Point is ” << PtInRect( rect, pt ) ? “” : “not”
<< “ in rectangle.” << endl;
As the preceding code illustrates, operations (such as assignment and argument passing) on class-type objects are subject to the same type checking as objects of built-in types.
Because the compiler distinguishes between class types, functions can be overloaded on the basis of class-type arguments as well as built-in type arguments. For more information about overloaded functions, see “Function Overloading” in Chapter 7, on topic and Chapter 12, “Overloading.”