WTF are header files exactly?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 2034 Views - Last Post: 27 August 2009 - 12:06 PM Rate Topic: -----

#1 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

WTF are header files exactly?

Post icon  Posted 26 August 2009 - 08:48 AM

As some of you may or may not know, I actually started to learn cpp before Java *gasp* but there were some concepts I just couldn't grasp. A few of them were headers and pointers. Now, right now I just want to grasp the header file thing. Is it like an interface? A class? What? In lamemans terms plzkthnx.
Is This A Good Question/Topic? 0
  • +

Replies To: WTF are header files exactly?

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: WTF are header files exactly?

Posted 26 August 2009 - 08:53 AM

Headers are simply source files. They typically contain class declarations or 3rd party libraries. When you use the preprocessor directive #include "fileName.h" the compiler goes through and replaces that sentence with the actual source. You could technically put it all in one file, but using headers allows for program separation and organization.

I wrote about header organization a few days ago.

To clarify, you can put whatever you want in a header (it could even be empty, but that would be pointless), but there are "recommendations"/standards of what "should" go in there.

This post has been edited by KYA: 26 August 2009 - 08:53 AM

Was This Post Helpful? 1
  • +
  • -

#3 Guest_Neumann*


Reputation:

Re: WTF are header files exactly?

Posted 26 August 2009 - 08:53 AM

Before using functions, you must declare them, describe what they take, what they return. Same thing with classes, before you use them you must declare them. The code for those functions/classes may be stored in another translation unit, which might be already compiled. So compiler may not be able to access them for their declarations. You need to explicitly state what you're about to use.

The headers are the files that contain such declarations.

This post has been edited by Neumann: 26 August 2009 - 08:56 AM

Was This Post Helpful? 0

#4 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: WTF are header files exactly?

Posted 26 August 2009 - 09:20 AM

basically, when a header file is included into a project like #include <header.h> basically the compiler copies the contents of header.h and pastes them in source.cpp

it's that primitive. in "more advanced" (not really- just prettier :D) languages like Java and D, when you use an import directive it doesn't copy and paste the contents but instead imports the symbols... so the behavior is *slightly* differant.
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: WTF are header files exactly?

Posted 26 August 2009 - 09:22 AM

<> denotes compiler's include directory, standard headers/libraries go here

"" quotes indicate that the header is in the local directory
Was This Post Helpful? 0
  • +
  • -

#6 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: WTF are header files exactly?

Posted 26 August 2009 - 09:23 AM

So it's basically an interface file?
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: WTF are header files exactly?

Posted 26 August 2009 - 09:23 AM

View Postpolymath, on 26 Aug, 2009 - 10:20 AM, said:

it's that primitive.


I find myself saying that about C++ a lot, in general. :lol:

View PostNeoTifa, on 26 Aug, 2009 - 10:23 AM, said:

So it's basically an interface file?


Kinda. You can think of it as a Java source file that contains just a class...no main method. Then when you use the #include, it's like import.

:)

This post has been edited by Locke: 26 August 2009 - 09:24 AM

Was This Post Helpful? 1
  • +
  • -

#8 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: WTF are header files exactly?

Posted 26 August 2009 - 09:26 AM

OH! Ok thanks. <3

Now hows about them pointer things?

This post has been edited by NeoTifa: 26 August 2009 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: WTF are header files exactly?

Posted 26 August 2009 - 09:27 AM

View PostNeoTifa, on 26 Aug, 2009 - 09:23 AM, said:

So it's basically an interface file?



No! An interface has method declarations, but other java files do not explicitly include it aside from saying they implement it. If a java class implements an interface it MUST override each method with one of its own. In C++, any file that needs the declaration of it includes it (see my link above). Headers lay out the framework for other files. Main.cpp needs to know what a Cat is, but doesn't care about the implementation, it will look in the Cat.cpp object file (created by the compiler, similar to .class files in java) for what a Cat actually does.

Definition versus declaration. You never include other .cpp files, so if you need one file to know what another does, have it include its header. (I'm a big fan of matching named headers/cpp files).

Hope that helps. If it's still as clear as mud, let me know.
Was This Post Helpful? 0
  • +
  • -

#10 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: WTF are header files exactly?

Posted 26 August 2009 - 09:32 AM

View PostKYA, on 26 Aug, 2009 - 10:27 AM, said:

(I'm a big fan of matching named headers/cpp files).


Anyone that doesn't practice this needs to be shot on sight. It's so difficult to keep track of them when they're named differently...it's pathetic.

This post has been edited by Locke: 26 August 2009 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#11 polymath  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: WTF are header files exactly?

Posted 26 August 2009 - 09:56 AM

View PostLocke, on 26 Aug, 2009 - 11:23 AM, said:

View Postpolymath, on 26 Aug, 2009 - 10:20 AM, said:

it's that primitive.


I find myself saying that about C++ a lot, in general. :lol:


primitive, but elegant in it's simplicity and efficiency. just finishing your sentences for you :D

serious now...

All a pointer is is basically a special kind of integer. You can imagine your computer's memory as consisting of a series of blocks. a pointer stores the number of one of those blocks. now why would this be useful?

A couple of reasons. when you call a functions with normal ints, it makes a copy of the variables. so in C (ie before reference variables like int&) you needed to pass a pointer so two functions could have access to the same memory.

The other place where you need to make an array of dynamic length. if you want an array that is equal in length to an int i (this was inputed by the user earlier. just go with it), you can't int array[i]; because the compiler can't make a static array like that. instead you say int * i = new int[i];. what this does is makes a pointer and then the new statement finds enough memory to fit i integers. it then sets the number of this block to i. then you can use it like any other array as long as you call delete i. if you need me to be more complete just reply.
Was This Post Helpful? 0
  • +
  • -

#12 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: WTF are header files exactly?

Posted 26 August 2009 - 10:22 AM

............ :blink: ummm please! I'm a retard. I took me a while before I even comprehended the significance of compilers! Omg thats another thing I'm gonna need explained: decompilors. XD But lets just focus on pointers. Explain it to me like you would your mom. I'm a retard.
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: WTF are header files exactly?

Posted 26 August 2009 - 10:49 AM

Memory (RAM) is a series of cubby holes.

Story: A pointer is in a cubby hole. It can hold the location of another cubby hole. In the pointer's cubby hole I put a piece of paper that tells me where an item is (in another cubby hole). I follow the directions on the piece of paper to get to the item I want. If I retrieve the contents of the pointer's cubby hole all I get is the piece of paper (memory address). If I dereference the pointer, I get what the piece of paper leads to.

This post has been edited by KYA: 26 August 2009 - 10:52 AM

Was This Post Helpful? 0
  • +
  • -

#14 Nizbel99  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 117
  • Joined: 19-May 09

Re: WTF are header files exactly?

Posted 26 August 2009 - 10:59 AM

Hello,

The cubby hole analogy was actually pretty good. What I also find good is seeing it graphically.
http://richardbowles...m/cpp/cpp18.htm

Try to think of pointers in a box and pointer diagram situation. If you retreive the value of the pointer (an arrow),
you'll just get the location of another value. But if you dereference the pointer, you can obtain the information
stored where the arrow is pointing. It's really similar to what KYA said, but it might help you understand :)

Zach
Was This Post Helpful? 0
  • +
  • -

#15 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2586
  • View blog
  • Posts: 15,618
  • Joined: 24-September 08

Re: WTF are header files exactly?

Posted 26 August 2009 - 11:50 AM

Oh, denied. I can't look at it until my mom stops being a twat. Copy and paste w/ attaching pic? Thanks. <3
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3