I'm starting to learn C++ and just out of curiosity, why does C++ require you to put a ";" at the end of the class declaration for example:
class A
{
/*...*/
};
In languages like java, it's used to signify an end of a statement.
What is different about
int i(5);
and the class above with regards to the semi-colon in C++? Does the compiler treat the class as a statement or does it have a different interpretation for it?
From a historical perspective it makes good sense, and if C++ allows all C grammer, and C++ classes are synonomous with structs, we are left with the necessary semi-colon at the end of the class.
In short, the number of . class files created will be equal to the number of classes in the code. We can create as many classes as we want but writing many classes in a single file is not recommended as it makes code difficult to read rather we can create a single file for every class.
The class declaration component declares the name of the class along with other attributes such as the class's superclass, and whether the class is public, final, or abstract.
A class is defined in C++ using keyword class followed by the name of the class. The body of the class is defined inside the curly brackets and terminated by a semicolon at the end. class className { // some data // some functions };
because you can also define variables in the declaration:
class A {
...
} x, y, z;
Ending class declarations in a semicolon is basically saying explicitly that we are not declaring any variables of this type.
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