Header files!

I have a program called, "Appcenter." I have already sectioned off code into .cpp files. How do I write a header file for these so that I can include them? What should I put in my header files? Examples would help, if you have them.

By the way, I use XCode 3.1.3.

Thanks in advance.

Gobblewobble123
Header files should consist of function and class declarations. Possibly macros and globals and typedefs if you need any.

ex:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//header
class A
{
private:
type member1;
type member2;
public:
A();
type method1();
void method2()
};
//.cpp file
A::A() //constructor stuff
{
}
type A::method1()
{
//method stuff
}

void A::method2()
{
//method stuff
}
So, and example of this would be:

Gamecenter. cpp
class blackjack
{
public:
int money;
int play_blackjack();
}blackjack;

class numbergame
{
int randomnumber;
int guess_number();
}numbergame;



Would this work as a header file? If not, then what needs to be added?

Thanks for the comment.

Gobblewobble123
Well, your numbergame class would be pretty useless as it's contents are completely private, your blackjack classes randomnumber shouldn't be public, and in general you should provide a constructor for your class, and if you have any dynamic content in your class and/or are going use it as a base class, they should also have virtual destructors. Also, the blackjack between the closing bracket and the semicolon means nothing, that is from C-Style structs you had to use typedefs on. Same with numbergame.

Other than that, yes you could use that as a header file.
Last edited on
No, you cannot use that as a header file. It declares an instance of each of the classes and that will violate the one definition rule.

(It will appear to work as a global if you only include it in one object file, but as soon as you go to reuse it within the project, the linker will not be able to resolve the ambiguous symbols.)
Also, the blackjack between the closing bracket and the semicolon means nothing, that is from C-Style structs you had to use typedefs on.

Actually, it does mean something: it instantiates an object of type blackjack named blackjack, clouding the name of the type.
Nice catch, filipe. I missed that they were both identical.
Actually, it does mean something: it instantiates an object of type blackjack named blackjack, clouding the name of the type.
Well yeah, what I meant was it doesn't do anything useful so it should be removed :O
Topic archived. No new replies allowed.