Share via


class

C++ Specific

class [tag [:base-list ]]
{
member-list
} [declarators]
;

[ class ] tag declarators**;**

The class keyword declares a class type or defines an object of a class type.

The elements of a class definition are as follows:

tag

Names the class type. The tag becomes a reserved word within the scope of the class.

base-list

Specifies the class(es) from which the class is derived (its base classes). Each base class's name can be preceded by an access specifier (public, private, protected) and the virtual keyword. See the Table of Member Access Privileges for more information.

member-list

Declares members or friends of the class. Members can include data, functions, nested classes, enums, bit fields, and type names. Friends can include functions or classes. Explicit data initialization is not allowed. A class type cannot contain itself as a nonstatic member. It can contain a pointer or a reference to itself. See the virtual keyword and the Table of Member Access Privileges for more information.

declarators

Declares one or more objects of the class type.

END C++ Specific

Example

In the following example, the class CAboutDlg is derived from the base class CDialog. The new class contains one member function named CAboutDlg:

// Example of the class keyword
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
   CAboutDlg();

// Dialog Data
   //{{AFX_DATA(CAboutDlg)
   enum { IDD = IDD_ABOUTBOX };
   //}}AFX_DATA

// Implementation
protected:
   virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
   //{{AFX_MSG(CAboutDlg)
      // No message handlers
   //}}AFX_MSG
   DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
   //{{AFX_DATA_INIT(CAboutDlg)
   //}}AFX_DATA_INIT
}

For more information, see struct, union, __multiple_inheritance, __single_inheritance, and __virtual_inheritance.