Advantes of Classes in header files?

Started by
6 comments, last by kaiel090x 22 years, 8 months ago
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
"He who fights monsters should look to it that he himself does not become a monster... when you gaze long into the abyss, the abyss also gazes into you."~Friedrich Nietzsche
Advertisement

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.

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



-Forcaswriteln("Does this actually work?");
Mostly I keep the header and the implementation file seperate unless of course if its a very small class .
I was influenced by the Ghetto you ruined.
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
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
#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.
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.


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.

This topic is closed to new replies.

Advertisement