473,395 Members | 1,335 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,395 software developers and data experts.

about the header file and source file in C++

Sorry about the beginners question

In the header file, each time you define a class or function, it has
to be followed by a semicolon.
But in the source file, the functions do not have to be followed by
semicolon.

What is the reason for this?

Thanks
Jun 27 '08 #1
6 1750
stonny wrote:
Sorry about the beginners question

In the header file, each time you define a class or function, it has
to be followed by a semicolon.
But in the source file, the functions do not have to be followed by
semicolon.
No. There should be no semicolons after a function body anywhere.
Inside a class definition you're allowed to put extraneous semicolons
but you do not have to put them there. Actually, I find the practice of
placing a semicolon after the body of a function defined inside a class
definition, irritating.
What is the reason for this?
A class definition is a declaration statement with the optional variable
name[s] you can place after the curly brace. A function definition is
not a declaration statement. It's a declaration followed by a compound
statement (a block surrounded by curly braces).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
stonny <zh************@gmail.comwrote in news:a4cda246-97f3-407a-a0dc-
2d**********@b64g2000hsa.googlegroups.com:
Sorry about the beginners question

In the header file, each time you define a class or function, it has
to be followed by a semicolon.
But in the source file, the functions do not have to be followed by
semicolon.
This holds only for class definitions, and has nothing to do with header
or source files.
What is the reason for this?
Support for inherited C syntax. In C it is common to write things like:

struct {int x, y;} my_point1, my_point2;
typedef struct Point_ {int x, y;} Point;

You see that you need a terminator for ending the whole construct. I
guess C++ could have managed with a syntax without semicolons if started
from scratch. Anonymous structs might come handy still sometimes though.

hth
Paavo
Jun 27 '08 #3
Paavo Helde <no****@ebi.eewrote in news:Xns9A9F588C6EB9Enobodyebiee@
216.196.97.131:
stonny <zh************@gmail.comwrote in news:a4cda246-97f3-407a-
a0dc-
2d**********@b64g2000hsa.googlegroups.com:
>Sorry about the beginners question

In the header file, each time you define a class or function, it has
to be followed by a semicolon.
But in the source file, the functions do not have to be followed by
semicolon.

This holds only for class definitions, and has nothing to do with
header
or source files.
>What is the reason for this?

Support for inherited C syntax. In C it is common to write things like:

struct {int x, y;} my_point1, my_point2;
typedef struct Point_ {int x, y;} Point;

You see that you need a terminator for ending the whole construct. I
guess C++ could have managed with a syntax without semicolons if
started
from scratch.
I would like to clarify my "I guess" statement. I meant that I believe
C++ syntax would have remained unambigous if the optional identifier
names and ending semicolon would have been dropped from the syntax of the
'class' construct. This would make it either inconsistent with 'struct'
or, if 'struct' were changed likewise, incompatible with C.

Regards
Paavo
Jun 27 '08 #4
Also, I find that semicolons make the code more readable(which is
always a good thing in C++). If you are reading a mess and notices a
{};(without first seeing where it starts) you know that´s the end of a
class, enum or struct...
Jun 27 '08 #5
Victor Bazarov wrote:
stonny wrote:
>Sorry about the beginners question

In the header file, each time you define a class or function, it has
to be followed by a semicolon.
But in the source file, the functions do not have to be followed by
semicolon.

No. There should be no semicolons after a function body anywhere.
Inside a class definition you're allowed to put extraneous semicolons
but you do not have to put them there. Actually, I find the practice of
placing a semicolon after the body of a function defined inside a class
definition, irritating.
However, I can see how it can be a bit hard to understand where to put
semicolons and why. Take this example:

namespace N
{

class C
{
void f()
{
} // (1)

}; // (2)

void f()
{
} // (3)

} // (4)

At (1) you can choose whether to add a semicolon or not
At (2) you must put one
At (3) and (4), it's forbidden

Especially the difference between (1) and (3) is surprising.

Jun 27 '08 #6
On May 22, 4:38 pm, Rolf Magnus <ramag...@t-online.dewrote:
Victor Bazarov wrote:
stonny wrote:
Sorry about the beginners question
In the header file, each time you define a class or
function, it has to be followed by a semicolon. But in the
source file, the functions do not have to be followed by
semicolon.
No. There should be no semicolons after a function body
anywhere. Inside a class definition you're allowed to put
extraneous semicolons but you do not have to put them there.
Actually, I find the practice of placing a semicolon after
the body of a function defined inside a class definition,
irritating.
However, I can see how it can be a bit hard to understand
where to put semicolons and why. Take this example:
namespace N
{
class C
{
void f()
{
} // (1)

}; // (2)
void f()
{
} // (3)
} // (4)
At (1) you can choose whether to add a semicolon or not
At (2) you must put one
At (3) and (4), it's forbidden
Especially the difference between (1) and (3) is surprising.
But historically explainable: (3) is according to C; in the case
of (1), Stroustrup felt that this distinction was artificial,
and added an optional semicolon to the grammar.

IMHO, the simplest solution would just be to modify the grammar
so that an empty statement is a declaration, rather than an
expression statement, so that a semicolon may appear anywhere a
declaration may, rather than only where an expression statement
is allowed. Or simply say that an empty statement is an empty
statement (a new type of statement), and that it can occur
anywhere any other statement may occur.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #7

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

Similar topics

31
by: Steven T. Hatton | last post by:
If a header is not necessarily a source file, and the sequences delimited by < and > in header names aren't necessarily valid source file names, what exactly is a header? -- p->m == (*p).m == p.m...
60
by: Derrick Coetzee | last post by:
It seems like, in every C source file I've ever seen, there has been a very definite include order, as follows: - include system headers - include application headers - include the header...
18
by: John Smith | last post by:
Hi all What does the group think of the practise of including one header file from inside another? I have some legacy code where this has been done, and it creates a dependency on a module...
13
by: tsoukase | last post by:
Hello, two questions: 1) Why is a library a collection of compiled source-files while each header-file is a single source? Would it be more efficient if they were both either compiled or not?...
10
by: PCHOME | last post by:
Hi! Would someone please help me thess C error(in gcc on Linux)? The compiler continues to give me: readLP.o: In function `Input_Problem': readLP.o(.text+0x0): multiple definition of...
4
by: Generic Usenet Account | last post by:
In our environment we have several C++ header files that have some inline member function definitions. For some reason, I am unable to step through the source code of those member functions in my...
16
by: Martin Jørgensen | last post by:
Hi, Problem: ======== Some of my output functions are beginning to take pretty many arguments... I mean.... We're talking about 10-15 arguments :-) So I thought to myself, perhaps this is...
12
by: spibou | last post by:
I have two identical files , u1.c and u2.c which only contain the line typedef int Q ; When I issue "splint u1.c u2.c" I get u2.c:1:13: Datatype Q defined more than once A function or variable...
16
by: wdh3rd | last post by:
Hi everyone. I'm new to C and I have a few questions: I am making files for permutations and combinations. Files to be made are perm.c, perm.h, combo.c, and combo.h. Since both combinations...
3
by: KIRAN | last post by:
Hello all, My question is about the way of including header files(*.h) in source files (*.c) I have three folders, -build ( for project makefiles) -include ( for *.h files) -src (for *.c...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.