I was just thinking that why I need headers at all? In headers, most oftenly we have the declarations only. In .cpp we have the definitons of that .h file. But, why can't we write all the functions directly in .h file and include it in my project files? Why are we dividing the functions declarations and functions definitions? In header file, we have header guard which prevent to have the definitions again and again. So, there is no point to say that translation unit size would be increased. So, what is the exact reason? Please clarify. Thanks.
nitin1
15
Master Poster
Recommended Answers
Jump to PostBut, why can't we write all the functions directly in .h file and include it in my project files?
Because then linking would fail with a "multiple definitions" error if you include the same header into more than one compilation unit (i.e. if you include it from more …
Jump to PostWhy will I need to update each and every source file?
For fun, let's use
printf
as our example even though it's unlikely to change for the forseeable life of C.int printf(const char *fmt, ...); int main(void) { printf("Hello, world!\n"); return 0; } …
Jump to PostWhy will I need to update each and every source file?
If you only change the definitions (implementations) of the functions, but leave all the declarations (or the function names and signatures) intact, then you won't have to change anything in the rest of the source code. However, …
All 10 Replies
rubberman
1,355
Nearly a Posting Virtuoso
Featured Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
nitin1
commented:
Good point!
+4
sepp2k
378
Practically a Master Poster
nitin1
commented:
nice.
+4
nitin1
15
Master Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
nitin1
15
Master Poster
deceptikon
1,790
Code Sniper
Team Colleague
Featured Poster
mike_2000_17
2,669
21st Century Viking
Team Colleague
Featured Poster
rubberman
1,355
Nearly a Posting Virtuoso
Featured Poster
Lardmeister
461
Posting Virtuoso
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.