Sign in to follow this  
grizzlebee

Header files, C files

Recommended Posts

grizzlebee    133
I am new to C++ and am trying to figure out when to use .h or .c files when coding. If I want to create a file that handles reading in files from xml for example: Do I make a xmlreader.h or xmlreader.c file? If there are some good links explaining design of software using .h and .c files I would appreciate. Sorry for my poor english.

Share this post


Link to post
Share on other sites
I_Smell_Tuna    96
Both file extensions allow for any type of code although some people tend to put their global variables, typedefs, and structures in .h files and keep all their funtions in .c files.

Share this post


Link to post
Share on other sites
grizzlebee    133
Oh I see. I understand many people have a hard time organizing them in an efficient manner. I would like mine to be organized to be efficient and properly structured.

Share this post


Link to post
Share on other sites
haegarr    7372
Normally, you are making a header file _and_ an implementation file (to be precise, in the case of C++ you should prefer .cc or .cpp, or even .C as file name appender; .c is for pure C files).

It is definitely not only a question of personal preference. Header files are for inclusion. That is, many implementation files will use them. So, if you are doing things that cause the instantiation of symbols in a header file, you will end up with multiple definitions of the same symbol. And that will cause your linker to complain.

Programming in C/C++ means to distinct between _declaration_ of variables, structures, classes, functions, and so on (typically done in the header files), and the _definition_ of variables, classes, functions, and so on (typically done in the implementation file).

Exceptions of this thumb rule exists: inlined functions and class templates have to be defined in header files (or at least in files included from header files). But perhaps that may be out of scope of the question.


EDIT: BTW: 2 answers during my typing. Hmm. Must become faster ;-)

Share this post


Link to post
Share on other sites
If you are trying to make object oriented code:

Basically in C++, one .h file and one .c/.cpp file is considered a class.

You might have two files:

XMLParser.h
XMLParser.cpp

XMLParser.h will contain the class skeleton (data definitions and function prototypes) and any header files that the class is dependant upon:

#include<string>

class XMLParser
{

private:
string XMLData;

public:
XMLParser();
~XMLParser();
}

XMLParser.cpp will contain the class implementation. This is where you will code the actual functions. Note that XMLParser.cpp MUST have an include directive for XMLParser.h:

#include "XMLParser.h"

XMLParser::XMLParser()
{
//initialization code goes here
}

XMLParser::~XMLParser()
{
//cleanup code goes here
}

If you are trying to make structural code the same rules apply, except that the header file (.h) will contain your global variables, data structures and function prototypes:

#include<string>

#define SOME_CONST_OR_MACRO somevalue

string XMLData;

struct someDataStructure
{
int member1;
int member2;
}

void initializeSomeDataStructure(int parm1, string parm2);

And your .c or .cpp file is going to contain the function implementations:

void initializeSomeDataStructure(int parm1, string parm2)
{
//insert some code here...
}

Be careful, though, of using the .c extension when coding C++. They will work the same, but it is not considered proper C++. Also, be careful that you understand the difference between C++ and C.

Check this out ;) http://www.research.att.com/~bs/bs_faq.html#difference
It was written by the guy who invented C++.

I recomend his books also, you will become a demigod if you read them.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this