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.
  1. Creating Header File C
  2. C++ Header File Example
  3. How To Make Your Own Header File In Dev C++
  4. Create A Header Banner

Oct 24, 2017  How to write your own header file in C? As we all know that files with.h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. There is need to include stdio.h in our C program to use function printf in the program. Dec 01, 2013  Well I now know that my header file is working and my problem is due to the path. I just moved my header file to where the standard libraries are contained and my program compiled and produced the expected output. However, I would like to keep my classes in a separate location. Does anyone know how I tell Dev C where to find my header files? This is not a good practice. You are allowed only to place prototypes in your header files and not the functions. If you add these things to your header file then there is absolutely no need for us to use linkers at all. Header files are used to import libraries that are, sometimes, pre-compiled.

And although most of these threats exist on the Windows side, Mac users—especially those who do a lot of downloading—can never be too careful about possible new exploits.Little Snitch may also warn of connections when software on your computer is responding to normal incoming connection attempts. Little Snitch reveals all of these problems by asking you what to do about unexpected network connections. For example, nmbd (NetBIOS name server) is part of your Mac’s Windows-compatible file sharing feature, and it will attempt to respond whether a Windows user is legitimately trying to access your files, or a Windows virus that takes advantage of NetBIOS is attempting to attack. Little snitch 431 mojave.

C++ header file example
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.

Creating Header File C


C++ Header File Example

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.

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)
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).
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.

How To Make Your Own Header File In Dev C++


Create A Header Banner

Thanks for the feedback!
Coments are closed
Scroll to top