Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

.h vs .cpp - When to use them?


  • Please log in to reply
15 replies to this topic

#1 tavichh

tavichh

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 399 posts
  • Location:Delaware
  • Programming Language:C#
  • Learning:C++, PHP, JavaScript

Posted 04 November 2012 - 03:27 PM

I just started learning C++, the tutorial I watched does not explain exactly when to use them so yea, can someone please tell me when I should have a header file and when I should have a pre-processer?

Or better yet, can someone describe to me in plain English what exactly it is? I am that early in learning C++ :)

I believe a pre-processer is like:
#include whatever.h
but what's confusing me is that its an entire file in this case.
So yea, please help!
  • 0
Send a like my way if I helped you or if you found my post useful or even just enjoyed reading my post.
Thank you :)

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 04 November 2012 - 03:48 PM

The preprocessor is part of the compiler, it merges files together when it runs into #include directives. The .h file is the code to be included. The result is something like this:

You run the compiler on your .cpp file. The preprocessor looks at the contents, and substitutes out any #include with the contents of the referenced file, as well as replacing any macros.

The resulting (temporary) file is then run through the compiler, which converts it to byte code. This is then run through the linker, which turns it into an executable file.
  • 1

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 tavichh

tavichh

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 399 posts
  • Location:Delaware
  • Programming Language:C#
  • Learning:C++, PHP, JavaScript

Posted 04 November 2012 - 05:29 PM

The preprocessor is part of the compiler, it merges files together when it runs into #include directives. The .h file is the code to be included. The result is something like this:

You run the compiler on your .cpp file. The preprocessor looks at the contents, and substitutes out any #include with the contents of the referenced file, as well as replacing any macros.

The resulting (temporary) file is then run through the compiler, which converts it to byte code. This is then run through the linker, which turns it into an executable file.

So the .cpp will not have any "actual" code in it, just references?
Sorry if this is a stupid question but the tutorial I watched was not helpful at all.
  • 0
Send a like my way if I helped you or if you found my post useful or even just enjoyed reading my post.
Thank you :)

#4 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 04 November 2012 - 05:40 PM


The preprocessor is part of the compiler, it merges files together when it runs into #include directives. The .h file is the code to be included. The result is something like this:

You run the compiler on your .cpp file. The preprocessor looks at the contents, and substitutes out any #include with the contents of the referenced file, as well as replacing any macros.

The resulting (temporary) file is then run through the compiler, which converts it to byte code. This is then run through the linker, which turns it into an executable file.

So the .cpp will not have any "actual" code in it, just references?
Sorry if this is a stupid question but the tutorial I watched was not helpful at all.

The .cpp is where you put the main code, the actual program. The references are things you use to help the .cpp accomplish it's goal. For example, if I had a random program that printed the contents of a string, if I were going to be using a string, I would have to include (reference) string, so that the .cpp can use it to store the value, and then print it.
  • 1
Speaks fluent Java

#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 04 November 2012 - 05:43 PM

Usually, the .cpp files will have code, and the .h files will have template/class/function declarations.

Most of this comes down to understanding how C++ compilers generally work, which is often a bit of a mystery when you're just learning the language. I didn't even get into how the linker works if you have multiple .cpp files, which would be the case in a real-world project.

I think I good comparison is how Delphi handles a form-based project. It uses .dfm files to handle the form information (like where dropdowns and text fields and labels are located), and .pas files to handle the logic when you do things on a form (such as change the value in a dropdown). A .h file is similar to a .dfm, and a .cpp is similar to a .pas for most C++ programmers.
  • 2

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 04 November 2012 - 06:03 PM

Here's my two cents:

.h files tell you what you can use, .cpp files tell the compiler how to implement those things that the .h files say you can do. Oftentimes when third parties provide libraries to programmers, they give you a binary library and .h files, but not the original source code in the .cpp files. This way, they can hide how their libraries are actually coded, but still let you use them.

Make sense?
  • 1

sudo rm -rf / && echo $'Sanitize your inputs!'


#7 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 04 November 2012 - 06:09 PM

I have a question related to this topic. Last time I tried including an external .h file, there was a value in the main script and a value in the .h that had the same name, and they conflicted with eachother. When you include a .h, does it literally add the code to the script before compile time?
  • 1
Speaks fluent Java

#8 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 04 November 2012 - 06:38 PM

Yes. The .cpp file behaves as if the .h file was literally copied and pasted in where the #include was. With C++ you can create namespace to avoid this problem, but this isn't possible with C.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#9 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 04 November 2012 - 06:44 PM

Yes. The .cpp file behaves as if the .h file was literally copied and pasted in where the #include was. With C++ you can create namespace to avoid this problem, but this isn't possible with C.

Alright, thanks. That clears some of the frustration I was having with C++. Is there a limit to certain namespaces, or is it anything you type?
  • 0
Speaks fluent Java

#10 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 04 November 2012 - 06:55 PM

They have to be valid identifiers. For example, things declared in the C++ standard library are put in the namespace std (you can see this if you open the header files). This is why a lot of programmers put using namespace std; at the top of their programs. This pulls everything in the std namespace into the global namespace so you don't have to prefix everything with std::. Don't do this, otherwise you'll have the same problem you did above.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#11 Chall

Chall

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 349 posts
  • Location:Cedar Rapids, IA
  • Programming Language:Java
  • Learning:C, Java, C++, C#, Python, JavaScript, Assembly

Posted 04 November 2012 - 07:07 PM

They have to be valid identifiers. For example, things declared in the C++ standard library are put in the namespace std (you can see this if you open the header files). This is why a lot of programmers put using namespace std; at the top of their programs. This pulls everything in the std namespace into the global namespace so you don't have to prefix everything with std::. Don't do this, otherwise you'll have the same problem you did above.

So, is everyone wrong then, for using std?
  • 0
Speaks fluent Java

#12 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 04 November 2012 - 07:19 PM

Not wrong, it's just not good practice. It's kinda like forcing everyone to use their first names and only their first names - inevitably you're gonna have two people with the same name and you're not going to be able to tell who's who. The namespace is equivalent to a person's last name.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download