8.1 Overview

Class types are defined using the class, struct, and union keywords. For simplicity, types defined with these keywords are called class declarations, except in discussions of language elements that behave differently depending on which keyword is used.

Microsoft Specific

“Local class” declarations—class declarations made in block scope—are exported to file scope in Microsoft C/C++. Classes not declared within another class always have file scope.¨

Names of classes defined within another class (“nested”) have class scope of the enclosing class.

Syntax

class-name:
identifier

The variables and functions of a class are called members. When defining a class, it is common practice to supply the following members (although all are optional):

Class data members, which define the state and attributes of an object of the class type.

One or more “constructor” functions that initialize an object of the class type. Constructors are described in “Constructors” in Chapter 11, on topic .

One or more “destructor” functions that perform cleanup functions such as deallocating dynamically allocated memory or closing files. Destructors are described in “Destructors” in Chapter 11, on topic .

One or more member functions that define the object's behavior. These functions perform operations particular to objects of a specific class only.