Skip to main content

17.4) Class Definition Syntax


Class definitions have the following structure:

class ClassName
{
  public:
    method prototypes
  private:
    instance variables
};

The public section provides prototypes of methods - functions that belong to the class, define its behaviour and have implicit access its instance variables. The private section defines instance variables - variables that each object of the class possesses, whose values collectively define the state that the object is in.

A class definition and its method implementations can be put in the .cpp file that needs the class, before the point of use, but it is generally a much better idea to put the class definition in a separate header file and the method implementations in a separate source file, as this makes it possible to reuse the class in other programs.