0

Why is it that a major class should be defined in a header file, then given a source file to define it's functions?

2
Contributors
3
Replies
4
Views
12 Years
Discussion Span
Last Post by Dogtree
0

One good reason would be to hide the implementation. If any changes need to be made to the implementation then it makes sense to separate it into a single file so that any code that uses the header doesn't need to be recompiled. If you place declarations in a header file you can more easily break the one definition rule, so separating declarations and definitions will make writing correct code easier. If you place the entire class in your headers then compilation can also take longer.

0

Thanks. Here's another question that came to mind. When you have more than one source file, how are they typically compiled? Does it compile the file with main in it first? Does it compile them in alphabetical order? Or something different?

And what's the one declaration rule?

1

It depends on the compiler. Generally the files will be compiled in the order that they're listed when the compiler is run.

>>And what's the one declaration rule?
One "definition" rule. It means that you can declare an object as much as you want because a declaration only says "Hey, I exist", but you can only define it once because a definition actually makes the object exist by allocating memory and calling constructors. If there are multiple definitions then you'll get an error.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.