#### Archived

This topic is now archived and is closed to further replies.

This topic is 6038 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there any advantage of storing my class in a header file and using include as apposed to just keeping it in the same script as main? And, in that case. Couldnt I just make a c++ program in a bunch of header files, include them, then call inside one main program!?!?!!!???!?!?!???????? ~ from the depths of the ocean Edited by - kaiel090x on August 11, 2001 4:14:50 PM

##### Share on other sites

If you have a big program (with lots of classes) this will keep them more organized and managable. Ideally you should have a .h and a .cpp for each class.

You only have to recompile what you''ve modified. If you keep all your code in one big file, and change one aspect of one class, the whole file must be recompiled.

Easier reusability. For example, I have a Game Manager class that I use in all my projects. Just the same .h and .cpp file.

##### Share on other sites
Think about all the scrolling that header files could save you. I always put my method definitions in the same .h file as the class. That''s probably a bad thing, but it works for me.

-Forcas

"Elvis is alive. He is Barney the purple dinosaur. He is the pied piper that leads our children into the wages of sin and eternal damnation."

##### Share on other sites
Mostly I keep the header and the implementation file seperate unless of course if its a very small class .

##### Share on other sites
You can technically use the #include directive anywhere to include anything, it''s dumb textual replacement/insertion.

Convention(al wisdom) dictates that a header file and source file be created for each class. The biggest advantage is in improving compile speed. The header should be minimal, contain just declarations which are processed mutliple times. The source code has all the definition that takes time to compile, and is only compiled once.

Never #include a .cpp file. If you need to make a template class, make a .hpp file and put everything in it (templates are a special case). Add each .cpp file to your project; each cpp is compiled seperately and then linked together. Also stick a "#pragma once" at the top of every .h file.

Magmai Kai Holmlor
- Not For Rent

##### Share on other sites
#pragma once is compiler-dependent... the portable way is to use inclusion guards, thusly:

#ifndef MYHEADERNAME_H#define MYHEADERNAME_Hclass MyClass{...};#endif // MYHEADERNAME_H

When you come to splitting your code up into several CPP files, you''ll realise that you need classes in separate headers, otherwise you''re including code from all over the place and defining things twice, and so on. Bad news.

##### Share on other sites
But when I use the include header, wil I be able to call my class''s objects the same way as I would if the classwas in the script?

quote:
Original post by Kylotan
#pragma once is compiler-dependent... the portable way is to use inclusion guards, thusly:

#ifndef MYHEADERNAME_H#define MYHEADERNAME_Hclass MyClass{...};#endif // MYHEADERNAME_H

When you come to splitting your code up into several CPP files, you''ll realise that you need classes in separate headers, otherwise you''re including code from all over the place and defining things twice, and so on. Bad news.

##### Share on other sites
quote:
Original post by Anonymous Poster
But when I use the include header, wil I be able to call my class''s objects the same way as I would if the classwas in the script?

Yes. To call a function, all the compiler needs to know it the address of the function, what parameters it needs, and what it returns. It doesn''t need to know how the function works. This is why you can use the C standard library even though you can''t see the source code.

Check this for yourself if you like. Open up <stdio.h> and take a look. (Quickest way: type <stdio.h> into a Developer Studio window, right-click, and choose ''Open Document''.) Apart from a lot of preprocessor stuff, you''ll find some function prototypes, but no function bodies. The rest of the code doesn''t need to see the function body in order to call the function.

Therefore, to use a class in a source file, all the compiler needs to know is the basic makeup of the class (all the public and private variables) and the function prototypes for that class. The actual implementation goes in its own .CPP file and calling code never needs to see it.

When it comes to creating the final executable, all the code is linked together in one file, and function names are resolved to addresses in the file for you.