C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 29th, 2012, 10:12 PM
fishy8158 fishy8158 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2011
Posts: 30 fishy8158 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 19 m 48 sec
Reputation Power: 2
Noob Question on headers.

I have just finished many tutorials on C++ for beginners. And I have a lot of separate classes with individual topics. I am making a console application to launch them separately.
Question is, should I have one header file for each class I have?

ex.

Header:
FileIO.h
Random.h
Pointer.h
Main.h

SourceFile:
FileIO.cpp
Random.cpp
Pointer.cpp
Main.cpp

So in the Main.h , i would #include all the other headers, and in the Main.cpp , i just include the Main.h. Is this how it should be done? Thanks in advance.

Reply With Quote
  #2  
Old September 30th, 2012, 01:21 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,840 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 19 h 25 m 12 sec
Reputation Power: 1774
> Question is, should I have one header file for each class I have?
You can normally do away with main.h. Each class typically has it's own .h and .cpp file.

In main.cpp, just have the #includes for
FileIO.h
Random.h
Pointer.h
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old September 30th, 2012, 12:10 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,141 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 23 h 52 m 21 sec
Reputation Power: 1974
The entire idea of header files is to provide to a source file (ie, a .cpp) macros (#define), type declarations, extern'd global variables, and function prototypes from another source file.

That is what you're doing by placing each class declaration in a header file. Every source file that uses that class declaration must include its header file, but if a source file does not use a class, then it does not need to include that header file. For example, Random.cpp must include Random.h, because it implements that class. If Pointer.cpp also uses the Random class, then it must also include Random.h, but if it doesn't use it then it won't (though including it won't hurt except to slow down compile time). If Main.cpp uses the Random class, then the same applies.

Only those source files that need to share information with other source files need to have a header file. What is in Main.h? Does it have type definitions that any of the other source files need? Does it have macros that the others need? Does it have the prototypes of functions in Main.cpp that the others need? Or are the only functions that Main.cpp contain main() itself, plus maybe a few other functions that are only called from main()? If Main.cpp has no information that the other source files need, then there is no need for Main.h.

BTW, it is generally a good practice to have one header file for each class. Though if a class is only used by one another class, it would make sense to place them both in the same header file and to place both of their implementations in the same source file. Whatever works best.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Noob Question on headers.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap