Hi All,
I am literally just beginning learning C++, following a beginners 21 day tutorial... so have no one I can ask these questions - other than you all!
I'm learning about 'class declaration and function definition'...
So far my .cpp files have contained the class declaration and the class method / function definition... now I'm splitting the class declaration into a header file and leaving the fuction definition in my .cpp file along with my main() function... This seems silly as each time I would ever want to use the header file and #include it in any new .cpp file I create, I would have to list all the function definitions again...
I was under the impression creating a header file was so others using the class (from the header file) wouldn't need to know how the functions work internally but could work out enough from the header file to realise what functions / methods a class has and is available for them to use... but if they then need to code all the function definitions into their .cpp file it seems pointless.
In the real programming world, would you create and declare a class in a header file and create only those function definitions in its related .cpp file ie the file wouldn't contain a main() function etc
I was wondering if I'm simply getting confused because all the .cpp files we create in the tutorial obviously start with main() to demonstrate the particular issue we are studying.
I am literally just beginning learning C++, following a beginners 21 day tutorial... so have no one I can ask these questions - other than you all!
I'm learning about 'class declaration and function definition'...
So far my .cpp files have contained the class declaration and the class method / function definition... now I'm splitting the class declaration into a header file and leaving the fuction definition in my .cpp file along with my main() function... This seems silly as each time I would ever want to use the header file and #include it in any new .cpp file I create, I would have to list all the function definitions again...
I was under the impression creating a header file was so others using the class (from the header file) wouldn't need to know how the functions work internally but could work out enough from the header file to realise what functions / methods a class has and is available for them to use... but if they then need to code all the function definitions into their .cpp file it seems pointless.
In the real programming world, would you create and declare a class in a header file and create only those function definitions in its related .cpp file ie the file wouldn't contain a main() function etc
I was wondering if I'm simply getting confused because all the .cpp files we create in the tutorial obviously start with main() to demonstrate the particular issue we are studying.
- How To Create A Header File In Dev C Free
- How To Create A Header
- C++ Header Files List
- C++ Header File Example
Mar 25, 2011 ok,lets,here we use Dev C to make header file. 1) Open the Dev c and Create new Console application and save it in new folder. 2) Now,time to the Write a Code. Ok, When you Create new Project than main.cpp file create by default, in this file Write following codes. Header files in dev-C. Ask Question Asked 11 years ago. Active 2 years, 3 months ago. Just make Your header file and save it as.h extension.
g++ main.cpp file.c file.h Only main.cpp and file.cpp will be compiled. A side effect of this is that header extensions are arbitrary. |
I wasn't sure that was the case. iirc, you could compile headers in VS. I haven't tried it since i switched to CodeBlocks+GCC. But that's a valid point.
About section 7 |
Oh crap! That's what i get for not testing enough. You're totally right, forward declaring works fine. Only problem happens if its implicitly inlined, but that's another matter.
Finally, about templates, I'd say it's better practice to put the template definition in the class declaration. |
How To Create A Header File In Dev C Free
Well -- I'm not a big fan of putting implementation in the class itself (unless it's a really small get() function or some other kind of 1-liner). I guess with templates it's alright because any dependencies can be forward declared and included after the class body (at least I think so, I'd have to actually test that).
There are other considerations, too, though. Like if the template class is exceedingly large and you want to ease compile time (though it would have to be pretty freaking big to make a difference)
How To Create A Header
Anyway overall I agree. I just included that bit out of completeness. I figured I should focus more on the instantiating method since everybody knows how to do the inlining method. But really -- the more I think about it, the more I think that should belong in another article (like one specifically talking about templates).C++ Header Files List
In response to that, I've decided to cut sections 7 and 9 completely, and touch up a few related things. I'll edit the posts once I get it straightened out on my local copy.