Question for C++ programmers

What do you put in the .h (header) file vs. what do you put in the .cpp file? 

I have been doing some win form applications in managed C++ and it puts most of its code that I thought would be in the .cpp file in-line in the .h file...but since I am just learning C++, I thought I would ask.

posted on Wednesday, August 04, 2004 5:26 AM

Feedback

# re: Question for C++ programmers

Now, as far as the compiler is concerned, there is no difference between .h & .cpp files. All the #includes get merged into one big file during the preprocessor step.

The division is done strictly for the simpicity of humans (those which happen to be C/C++ programmers). Having different pieces in different files often makes things easier to organize.

However, when you are using VisualStudios Wizards it's organizational needs superceed yours (since it's the one doing the organization for you). So, strange things end up in the .H files.

So, what would normally be placed in a .H file. When most programmers are asked this, they reel off a list of examples. I once put that question on an interview exam I gave potenial hires, and had to rephrase it three or four different way to get people to STOP giving me a list of example. Virtually no one could explain, conceptually, what belongs in a .H file.

OK, so here's the answer: Normally, you put in a .H file, that information which must be SHARED among different modules in the system. Declarations feed to be shared, definitions do not (and so on)
8/4/2004 6:38 AM | James Curran

# re: Question for C++ programmers

I always thought of it as a matter of visibility and organization. You put code in a .h file that you want visible to other code in your application. Be it a type/class/method declaration, an inline method, or a constant. With regard to organization, it does often help to break implementation details into a separate file, especially as an application begins to grow. I'm sure there are a lot more great reasons for header files that I'm just forgetting.

With a quick bit of Googling, I came across this article that I think might answer some of the questions you have about header files.

http://www.gamedev.net/reference/programming/features/orgfiles/page2.asp

I just tried using the "Add Function" feature in VS.NET with a C++ WinForms application and I believe I'm seeing the same behavior you are. I'm not sure why the IDE puts the declaration and the definition in the header file. It does look different, but I believe it functions just the same as it would had the definition been placed in the .cpp file. Perhaps it has something to do with it being managed C++, though I can't think of a reason why that would be the case. One thing I noticed is that having the declaration and definition in one place sure makes C++ look a lot more like C#. ;-)

P.S. - I'm sure you've heard this before, but if you're just getting started in C++ I recommend "C++ Primer" by Lippman. I used it to learn unmanaged C++, but I'm guessing a good portion of it still applies.
8/4/2004 6:40 AM | Eric Tobia

# re: Question for C++ programmers

Jame and Eric: Thanks for the replies, that helps a little. Seems weird to have the code in the .h file.

Once I get done with this managed C++ book, I'll be going back to plain on C++ and hopefully that will be a little different. I also have a VC++ 6.0 book that I might finally get back to.
8/4/2004 12:53 PM | Jason Haley

# re: Question for C++ programmers

what is the 5 major difference in the structures and classes answer me at roshu4u46@yahoo.com i will be very thankful to u
2/14/2005 10:04 PM | muhammad rashid

# re: Question for VC++ programmers

hi,
i need some question on VC++ for my interview preparation
with regards,
bharath
2/27/2005 8:25 PM | B.Bharathkumar

# re: Question for C++ programmers

student registration in university?
3/9/2005 11:22 PM | awwady

# re: Question for C++ programmers

student registration in university?
3/9/2005 11:24 PM | awwady

# re: Question for C++ programmers

student registeration ?
3/9/2005 11:26 PM | awwady

# re: Question for C++ programmers


tHE HEADER FILE #INCLUDE <BITSET> OR <BITSET.H>
ARE NOT VALID IN MY COMPUTER AND i LIKE TO USE ALL COMAND FOR BITWISE OPERATION,WHAT THE DETAIL ADVICE FOR ME TO MAKE THIS BITWISE OPERATIONAL.
THANKS AND BEST REGARDS
MIRGHANI TAJELSIR
eMAIL MTAJELSIR1.KHARTOUM@GNPOC.COM
3/20/2005 1:40 AM | MIRGHANI TAGELSIR

# re: Question for C++ programmers

hi,
I need some question on C++ for my interview preparation
with regards,

somraj
3/26/2005 1:34 AM | somraj pal

# re: Question for C++ programmers

There are no difference b/n structure and class except 'All members are public by default in strcuture, private in classes'
3/31/2005 12:09 AM | kcv

# re: Question for C++ programmers

There are no difference b/n structure and class except 'All members are public by default in strcuture, private in classes'
3/31/2005 12:09 AM | kcv

# re: Question for C++ programmers

A hospital has three wards, ward a, b and c. each ward has 10 rooms
Ward a:-first class
-only one bed in a room
-fee is birr 100 per day
Ward b:-second class
-Two beds in each room (one bed may be added in special circumstances)
-fee is birr 50 per day
Ward C: -third class
-Four beds in each room (two beds may be added in special circumstances
-fee is birr 20 per day
The hospital has enough resources to treat 70 patients at a time. It has exactly 70 beds, each having a unique identification number. In normal situations, there are 1, 2, and 4beds in each room in wards A, B and C respectively.
Sometimes, there may be an empty bed in a room, but a patient can not be assigned to it because she/he is of a different gender. In such situations, the bed is moved to one of the other rooms and the patient is assigned to it.
The hospital needs to maintain patient information in three categories:
1) admitted patients –those who are currently occupying a bed in the hospital
2) released patient –those that have been released from the hospital after treated
3) waiting patients-patients who need to be admitted to the hospital, but waiting for a free bed
Patients are transferred from one category to another in one of the following cases:
1) When the current patient is released after being treated.
2) When a bed is found for a waiting patient.
Some of the tasks your program is expected to handle are:
1. Search for patient information:
- Given the name of the patient, identify the ward, room and bed she/he is occupying.
-Given the word, room and bed, identify the patient occupying it by name.
2. Search for free beds in a specific room, in a given ward or in the hospital
3. Bed allocation
-assign a patient to abed
-remove a patient from a bed
-transfer a patient from one bed to another
4. Admit/relasse a patient
-register a patient
-admit a patient or add to waiting list
-release a patient
5. Calculate a patient’s bill
6. Your program should be file based


4/24/2005 11:41 AM | elias

# re:Question for C++ programmers

hi,
i need some question on VC++ for my interview preparation
with regards,
arvind
4/27/2005 3:29 AM | arvind

# re: Question for C++ programmers

I Wwant to know c++ interview queistion
5/16/2005 6:45 PM | INTERVIEW QUEISTION

# Question for C++ programmers

Hi,
I want to know C++ interview questions.
Regards,
siva
6/26/2005 10:27 PM | siva

# re: Question for C++ programmers

what is difference between refrence & pointer in c++?
2/21/2006 10:19 AM | kumar manish

# re: Question for C++ programmers

Hi,
I want to know C++ interview questions.
Regards,
6/9/2006 12:31 AM | sandeep

# re: Question for C++ programmers

hai
I want to know how c++ program will interact with oracle in linex platform.
please send which books I will refere
6/16/2006 2:46 AM | baswaraj

# QS: Question for C++ programmers

i want to know the best book to strt with for vc++/mfc ie .net tech.so i need some good recommends for the books plz..as im a begginer in vc++ .net etc...
8/22/2006 10:12 PM | ZANG

# re: Question for C++ programmers

one more qs,what is the difference betwn the c++ and vc++....
8/22/2006 10:13 PM | ZANG

# re: Question for C++ programmers

<a href=http://www.emicrosoftinterview.com>Microsoft Interview Questions</a> is complete interview guide for technical questions, hr questions, puzzles, discussion forumns.
10/1/2006 10:06 PM | Steve Smith

# re: Question for C++ programmers

http://www.emicrosoftinterview.com is complete interview guide for technical questions, hr questions, puzzles, discussion forumns.
10/1/2006 10:06 PM | Steve Smith

# re: Question for C++ programmers

hi
11/20/2006 5:37 AM | vijay

# I want some vc++ interview question with Answer

Hi
I am working in vc++ domain.so i want some vc++ interview question with answer
bye
12/16/2006 5:35 AM | Balasingam

# re: Question for C++ programmers

Many C/C++ interview questions with answers can be found at http://www.technical-interview.com
2/16/2007 11:11 PM | CPP

# re: Question for C++ programmers

Hi,
I am working in vc++ domain.so i want some vc++ interview question with answer
bye
3/25/2007 10:55 PM | vanajak

# Question for C++ programmers

How to activate hot keys in propertypage, defined in accelerator
11/16/2007 12:46 AM | Ganesh

# re: Question for C++ programmers

How to activate hot keys in propertypage, defined in accelerator? Pls reply if any one knows solution about this.
11/16/2007 12:48 AM | Ganesh

Post Comment

Title  
Name  
Url
Comment   
Please enter the following code into the box below to stop spammers

  
Enter Code Here *