Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does C++ have a ";" after a class declaration [duplicate]

Tags:

c++

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?

like image 501
sameday Avatar asked Oct 03 '13 16:10

sameday


People also ask

Does a class end with in C++?

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.

Can a class be declared more than once?

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.

What is a class declaration?

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.

How is class declared in C Plus Plus?

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 };


1 Answers

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.

like image 186
green lantern Avatar answered Oct 11 '22 13:10

green lantern