.h files.....

Gibson486

Lifer
Aug 9, 2000
18,378
1
0
i am finishing my data structure and algorithm class (it's more of a "why use Java instead of C++ class" ) and we are ending the class on Java to C++ conversion. I took a intro to C++ class before and I never had to make a .h file. So, my question is, why cant you just combine everythign into the .cpp file?
 

fs5

Lifer
Jun 10, 2000
11,774
1
0
you can but classically their seperated so that you can distribute the .h (header) file with a complied .cpp file and thereby "hiding" your implementation methods.

There's probably another reason but that's all I know.
 

RagingBITCH

Lifer
Sep 27, 2003
17,619
2
76
Originally posted by: Gibson486
Object Oriented Programming.

ummm...that was really no help....

It wasn't meant to be :p

Tell me - why the hell do you have #include <whatever.h> at the top of all your C++ programs? Have you tried using cout without the #include? It should be fairly evident, in addition to what five said.
 

Header files are one of the bad things about C/C++. Yes, they are good for documenting interfaces, but what a PITA to update two files just to make a new method. I don't suggest globbing everything into your .c/.cpp files, do the header thing -- but it's a pain.

 

Gibson486

Lifer
Aug 9, 2000
18,378
1
0
ehhh....i know you have to include the .h in the header of the .cpp file...but that's not my point or question. My question was why do you bother making a seperate .h file as if it was a seperate package? As far as I remember, when i took my C++ class, everything just went into the .cpp file.
 

bleeb

Lifer
Feb 3, 2000
10,868
0
0
Originally posted by: RagingBITCH
Object Oriented Programming.

Try working on a project with 100 people BUSTING out code.... and taking 3-4 years to complete the project.
 

fs5

Lifer
Jun 10, 2000
11,774
1
0
Originally posted by: RagingBITCH
Originally posted by: Gibson486
Object Oriented Programming.

ummm...that was really no help....

It wasn't meant to be :p

Tell me - why the hell do you have #include <whatever.h> at the top of all your C++ programs? Have you tried using cout without the #include? It should be fairly evident, in addition to what five said.

I think he isn't questioning why he needs to include header files, he's complaining why you need to use 2 files instead of just 1.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Large projects have hundreds of CPP and H files. At work VC++ loads over 1,000 files on opening the workspace.

H file = definition, contract for behavior, the part you need to know about to use the functions / classes

CPP file = implementation, which users of the functions / classes should never need to look at.

Library function definitions belong in H files so that the H can be included into may different files that use that library. You don't read the source code for STL/standard library or math functions, you read class and function definitions so your code knows what atoi() is and how it's supposed to be called.

Your own class definitions (but not code) belong in H files so that your other classes and functions in other CPP files can use your classes.
 

RagingBITCH

Lifer
Sep 27, 2003
17,619
2
76
Originally posted by: fivespeed5
Originally posted by: RagingBITCH
Originally posted by: Gibson486
Object Oriented Programming.

ummm...that was really no help....

It wasn't meant to be :p

Tell me - why the hell do you have #include <whatever.h> at the top of all your C++ programs? Have you tried using cout without the #include? It should be fairly evident, in addition to what five said.

I think he isn't questioning why he needs to include header files, he's complaining why you need to use 2 files instead of just 1.

Well you answered that question :)
 

Stealth1024

Platinum Member
Aug 9, 2000
2,266
0
0
I'm taking CS4, a C++ class after CS 1 to 3 were java, and since this is the first week I'm not sure yet, give me another week.... lol

 

Gibson486

Lifer
Aug 9, 2000
18,378
1
0
Well, I know what each file does and what each conatins, but i am still confused on why you would still do it. My prof told us in class that C had the two files because it increased effeciency. I dont know if it changed with C++, but people seem to have more of a liking to Java, which only uses one file, but is not as effecient. I dont know, I am more or less just confused because i never used .h in my C++ class.
 

mugs

Lifer
Apr 29, 2003
48,924
45
91
Information hiding... .h files make it easier for someone to see a list of the class methods and data and whatever documentation you want to put in there than looking through the entire .cpp file.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
In large projects each class often goes into its own pair of CPP & H files, to keep sizes manageable, make changes more visible and localized, and so that different people can work on different files instead of all trying to change one big monster file.

Why not just a bunch of CPP files though? Aside from information hiding, the H file should change much less often than the CPP file. Keeping them separate makes it easier to tell when function or class definitons have changed (a big deal for anyone using the class or function) as opposed to the implementation changing (much more often, and something that code using the class in other CPP files should be able to ignore).

Also, for really elaborate classes there may be one H file and more than one CPP file. Consider a graphics transformation object where one team member is writing the JPEG encoding while another is writing the bilinear filtering for resizing. H file = CPicture.H, CPP #1 = CPictureEncoder.CPP CPP #2 = CPictureResizer.CPP.

(edit, yes I know in that case the encoder and resizer might be friend classes or non-class functions instead of part of the main class.)