Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i make a header file for a shopping mall project in C++?
Posted

You should first go back to your tutorials and learn C++. Nobody here will write the program for you, so ask specific questions. For instance, what is your problem exactly, what did you try yet and why it didn't work, ...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 2:52am    
Right, tell them! - my 5.
--SA
The general structure for C++ headers is

Start with a Macro Guard.
This can either be a #pragma once (Microsoft Visual Studio 7.1 or later only) or a #define
This stops any classes, functions and external variables getting defined multiple times if the header is included from multiple locations.
If you use the #define the variable defined must be unique for the entire solution, a common naming style is __HEADERFILENAME_H__ where HEADERFILENAME is the name of the .h file

Then you simply provide prototypes for the functions and definitions of classes, structs, enums, ...

MyHeader.h
#ifndef __MYHEADER_H__ //Must be unique
	#define __MYHEADER_H__ //Must match the previous line

	typedef struct {
		int nSomeVar;
	} SomeStruct;

	int SomeFunction();
#endif


The idea is that the code for int SomeFunction(); is in a different .cpp file than what you are trying to use it from
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 2:53am    
Useful - my 5. Also -- "pragma once"
--SA
Nuri Ismail 18-Jan-11 3:16am    
Andrew, the "endif" statement must be at the end of the file, like this:

#ifndef __MYHEADER_H__ //Must be unique
#define __MYHEADER_H__ //Must match the previous line

typedef struct {
int nSomeVar;
} SomeStruct;

int SomeFunction();

// At the end of the file
#endif // __MYHEADER_H__

This way all of the declarations and definitions will be guarded against multiple includes.
Andrew Brock 18-Jan-11 3:23am    
So it should, bit of a typo. The answer has been corrected. Thanks.
Nuri Ismail 18-Jan-11 3:24am    
Great, and have a 5. :)
You don't make a header file for "a project" (whatever you mean by one of those, it's an overloaded term) you make one to describe an interface to something. The idea is that a header file contains the minimum amount of information required to use something while being standalone and complete.

This interface could be:

- a set of functions and associated data types used in the interfaces to those functions (e.g. old C style stdio)

- a class definition (e.g. the C++ basic_string class)

- a set of class declarations used to pass incomplete or undefined objects through lumps of code that don't need to know what they are, just that they exist (e.g. the C++ standard library iosfwd)

- and a few more including macro expansions (for generative programming) and explicit template instantiations (for creating libraries from template classes for specific parameterising types), but if you don't know what a header file is I wouldn't worry about that yet.

Incidentally, you don't always need an include guard (the #ifndef FOO_/#define FOO_/#endif rubbish) other posters have mentioned. Files that only contain non-defining declarations or function prototypes don't need them. Although it might be a good habit to get into using them it's even better to understand where you need them and why.

Anyway, for the full lowdown on include files and how to use them in C++ go and buy yourself a copy of "The C++ Programming Language" by Bjarne Stroustrup (or get one from a library).

Cheers,

Ash
 
Share this answer
 
You put your class definition and function definition in the header files
it's recommended to use

#pragma once as your first line of header file. This will help you to include the content only once.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900