473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Why is ; used at the end of class definition.

Why is ; ~ semi-colon used at the end of class definition ?

Thanks JK

Jul 26 '07 #1
9 5055
gopal wrote:
Why is ; ~ semi-colon used at the end of class definition ?
It's a declaration statement. Like any statement it should
end in a semicolon. Why do you put a semicolon after 'a' here:

enum A { one, two };

?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 26 '07 #2
In article <11*********************@q75g2000hsh.googlegroups. com>,
go*********@gmail.com says...
Why is ; ~ semi-colon used at the end of class definition ?
Because the syntax allows you to define an instance of that type,
something like:

class X {
// ...
} x;

So, without the semicolon, the compiler assumes the next symbol it sees
should be the name of an instance. The semicolon tells it that the
definition is complete, and it shouldn't be looking for an instance
name.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 26 '07 #3

gopal wrote:
Why is ; ~ semi-colon used at the end of class definition ?

Thanks JK
C++ uses ; to tokenize the strings.
This means that until the C++ compiler sees ';'
The same goes for english, when we sees full stop, we know its the end
of that phrase.
Hope this helps!!!

Jul 27 '07 #4
gh********@gmail.com wrote:
C++ uses ; to tokenize the strings.
This means that until the C++ compiler sees ';'
The same goes for english, when we sees full stop, we know its the end
of that phrase.
Hope this helps!!!
Then why is there no ';' at the end of function implementations?
Jul 27 '07 #5
Juha Nieminen wrote:
gh********@gmail.com wrote:
>C++ uses ; to tokenize the strings.
No, the ; is a token. C++ uses it as a syntactic element.
>This means that until the C++ compiler sees ';'
Then what? You can write a lot of code without the use of any ';'.
>The same goes for english, when we sees full stop, we know its the end
of that phrase.
Hope this helps!!!

Then why is there no ';' at the end of function implementations?
Short answer: Because the syntax says so. A language's syntax rules are
different from language to language. A lot of languages have 'then' as a
keyword used with 'if'. Some languages use ':=' for assignment while others
use '<-'.

Much of the syntax for C++ is inherited from C, where 'struct' declarations
has a ';', and function definitions does not. In C++, the syntax for
classes is the same as for structs, except for the keyword.

Another answer:

class foo { }

is a type, and can be used like:

class foo { } a, b;

which also defines a and b to be of type foo.

void bar() { }

is not a type, however.

--
rbh
Jul 27 '07 #6

"Robert Bauck Hamar" <ro**********@ifi.uio.nowrote in message
news:f8**********@readme.uio.no...
Juha Nieminen wrote:
>gh********@gmail.com wrote:
>>C++ uses ; to tokenize the strings.

No, the ; is a token. C++ uses it as a syntactic element.
>>This means that until the C++ compiler sees ';'

Then what? You can write a lot of code without the use of any ';'.
>>The same goes for english, when we sees full stop, we know its the end
of that phrase.
Hope this helps!!!

Then why is there no ';' at the end of function implementations?

Short answer: Because the syntax says so. A language's syntax rules are
different from language to language. A lot of languages have 'then' as a
keyword used with 'if'. Some languages use ':=' for assignment while
others
use '<-'.

Much of the syntax for C++ is inherited from C, where 'struct'
declarations
has a ';', and function definitions does not. In C++, the syntax for
classes is the same as for structs, except for the keyword.

Another answer:

class foo { }

is a type, and can be used like:

class foo { } a, b;

which also defines a and b to be of type foo.

void bar() { }

is not a type, however.
So does the ';' at the end of a class/struct definition make it easier to
parse or is it unnecessary? (I'd tend to think that it is unnecessary since
the brackets are there).

John

Jul 28 '07 #7
JohnQ wrote:
:: "Robert Bauck Hamar" <ro**********@ifi.uio.nowrote in message
:: news:f8**********@readme.uio.no...
::: Juha Nieminen wrote:
:::
:::: gh********@gmail.com wrote:
::::: C++ uses ; to tokenize the strings.
:::
::: No, the ; is a token. C++ uses it as a syntactic element.
:::
::::: This means that until the C++ compiler sees ';'
:::
::: Then what? You can write a lot of code without the use of any ';'.
:::
::::: The same goes for english, when we sees full stop, we know its
::::: the end of that phrase.
::::: Hope this helps!!!
::::
:::: Then why is there no ';' at the end of function
:::: implementations?
:::
::: Short answer: Because the syntax says so. A language's syntax
::: rules are different from language to language. A lot of languages
::: have 'then' as a keyword used with 'if'. Some languages use ':='
::: for assignment while others
::: use '<-'.
:::
::: Much of the syntax for C++ is inherited from C, where 'struct'
::: declarations
::: has a ';', and function definitions does not. In C++, the syntax
::: for classes is the same as for structs, except for the keyword.
:::
::: Another answer:
:::
::: class foo { }
:::
::: is a type, and can be used like:
:::
::: class foo { } a, b;
:::
::: which also defines a and b to be of type foo.
:::
::: void bar() { }
:::
::: is not a type, however.
::
:: So does the ';' at the end of a class/struct definition make it
:: easier to parse or is it unnecessary? (I'd tend to think that it
:: is unnecessary since the brackets are there).

No, it is necessary becase the class definition can optionally be
followed by declarations of variables of the class type. Look at foo
above, where the semicolon isn't terminating the class, but the list
"a, b". The semicolon must be there, even if the list is empty!
Bo Persson
Jul 28 '07 #8
JohnQ wrote:
>
"Robert Bauck Hamar" <ro**********@ifi.uio.nowrote in message
news:f8**********@readme.uio.no...
>Juha Nieminen wrote:
>>gh********@gmail.com wrote:
C++ uses ; to tokenize the strings.

No, the ; is a token. C++ uses it as a syntactic element.
>>>This means that until the C++ compiler sees ';'

Then what? You can write a lot of code without the use of any ';'.
>>>The same goes for english, when we sees full stop, we know its the end
of that phrase.
Hope this helps!!!

Then why is there no ';' at the end of function implementations?

Short answer: Because the syntax says so. A language's syntax rules are
different from language to language. A lot of languages have 'then' as a
keyword used with 'if'. Some languages use ':=' for assignment while
others
use '<-'.

Much of the syntax for C++ is inherited from C, where 'struct'
declarations
has a ';', and function definitions does not. In C++, the syntax for
classes is the same as for structs, except for the keyword.

Another answer:

class foo { }

is a type, and can be used like:

class foo { } a, b;

which also defines a and b to be of type foo.

void bar() { }

is not a type, however.

So does the ';' at the end of a class/struct definition make it easier to
parse or is it unnecessary? (I'd tend to think that it is unnecessary
since the brackets are there).
If by 'unnecessary', you mean: Could a compiler parse a syntax without
the ';' following a class definition? Then I believe the answer is yes it
is unnecessary, and that a correct program with this syntax would not be
easier nor harder to parse. But as C++ is a complex beast, I could be
wrong.

The technical syntax bit, however, is that a function definition is a
declaration by itself, while a class specifier (as the class foo { ... }
construct is called in the syntax) is a type specifier. Type specifiers can
also be 'int' or 'double'.

So when 'class foo {}' is parsed, the compiler would expect following tokens
in a same way as if 'int' had been parsed.

And to make it complete, here is an example of a function definition with a
class definition:

struct foo {} bar() {}

This is allowed by the syntax, but C++ has other rules to forbid it. My C
compiler compiled it, however. G++ gave an explanation that 'new types may
not be defined in a return type', which says 'g++ did parse it
successfully, but it will not let it compile'.

--
rbh
Jul 28 '07 #9
Robert Bauck Hamar wrote:
>>The same goes for english, when we sees full stop, we know its the end
of that phrase.
Hope this helps!!!
Then why is there no ';' at the end of function implementations?

Short answer
It was a rhetorical question.
Jul 28 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
3
by: Eric Lilja | last post by:
Hello, I have a few global variables in my program. One of them holds the name of the application and it's defined in a header file globals.hpp (and the point of definition also happen to be the...
6
by: Dan Sikorsky | last post by:
If we were to define all abstract methods in an abstract class, thereby making that class non-abstract, and then override the heretofore 'abstract' methods in a derived class, wouldn't that remove...
2
by: tony | last post by:
Hello! I'm trying to build a class library which has a class called AvestaPlantFunc. In this project building a class libray exist a class called AvestaPlantFunc. In this class is there a...
34
by: Guch Wu | last post by:
Boost has many terrific libraries. But I want to know whether they are ready for using in real projects. Which of them are mature enough, or just only in progress?
9
by: jerry.upstatenyguy | last post by:
I am really stuck on this. I am trying to write a string array containing a "word" and a "definition" to a class called Entry. Ultimately this will end up in another class called dictionary. No,...
6
by: djm | last post by:
hello everyone, im doing a c++ coursework which consists linked lists and use of classes. but im getting some compilation errors. can some please help me out. //this is the header file...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.