943,856 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3108
  • C RSS
May 17th, 2005
0

Classes and header files

Expand Post »
Why is it that a major class should be defined in a header file, then given a source file to define it's functions?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SquirrelProdigy is offline Offline
24 posts
since Nov 2004
May 17th, 2005
0

Re: Classes and header files

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.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 17th, 2005
0

Re: Classes and header files

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SquirrelProdigy is offline Offline
24 posts
since Nov 2004
May 18th, 2005
1

Re: Classes and header files

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.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Creating a Coordiantion (Grid Based) System for a text-based game.
Next Thread in C Forum Timeline: Reciving data w/ a network app





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC