Home Segments Index Top Previous Next

152: Sidetrip

Why, you might ask, do class definitions end with a semicolon. After all, the final brace makes it clear where the class definition ends.

class box_car { 
  public: 
    double height, width, length; 
}; 
 ^ 
 | 
 *-- Semicolon 

One reason is that the semicolon syntax allows you to describe a class and to define global variables using that class in a single statement. In the following, for example, in a single statement, the box_car class is defined and global variables b1 and b2 are defined that belong to the box_car class.

class box_car { 
  public: 
    double height, width, length; 
} b1, b2; 

Thus, the semicolon tells C++ where the list of defined variables ends.