In a C++ class declaration:
class Thing { ... };
why must I include the semicolon?
Role of Semicolon (;) in C language: The Semicolon tells that the current statement has been terminated and other statements following are new statements. Usage of Semicolon in C will remove ambiguity and confusion while looking at the code.
A semicolon after a close brace is mandatory if this is the end of a declaration. In case of braces, they have used in declarations of class, enum, struct, and initialization syntax. At the end of each of these statements, we need to put a semicolon.
Semicolon there is NOT optional.
A statement ends either with a block (delimited by curly braces), or with a semicolon. "do this while this" is a single statement, and can't end with a block (because it ends with the "while"), so it needs a semicolon just like any other statement.
The full syntax is, essentially,
class NAME { constituents } instances ;
where "constituents" is the sequence of class elements and methods, and "instances" is a comma-separated list of instances of the class (i.e., objects).
Example:
class FOO { int bar; int baz; } waldo;
declares both the class FOO and an object waldo.
The instance sequence may be empty, in which case you would have just
class FOO { int bar; int baz; };
You have to put the semicolon there so the compiler will know whether you declared any instances or not.
This is a C compatibility thing.
because you can optionally declare objects
class Thing { ... }instanceOfThing;
for historical reasons
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With