Hello,
A .h file is a header file, usually used to store function definitions.
So, say you are writing some sort of math library.
your .h file might look like:
//header file
int add(int a, int b);
int subtract(int a, int b);
//end header
the c++ file would expound upon these definitions, and contain the function 
body.
The header file can also be used in order to contain the methods for the class:
//header file:
class math {
int add(int a, int b);
int subtract(int a, int b);
};
//end header
Again, the c++ file would just expound on these classes, and provide the 
functions for the prototypes.
.h files are usually used in conjunction with libraries, in order to define 
constants and function prototypes, but limit the user to the code.
E.g, if you write a wrapper for some lower level functions, and decide to 
distribute that rapper as a library (either dynamic or static), you may not 
want everyone having your code.
By providing the header files, and allowing the programmer to link against your 
library you are doing that.

Header files can also be used as organization--it's much easier to read a 
header file and find constants that are needed, rather than to look through 
tons of code to find them.
Something that might be worth noting is the inclusion guard with header files.
My header files tend to look like this:
//header file example 3:
#ifndef example3_h
#define example3_h
...
#endif
//end header
What this does, is allows for inclusion guards.
You can include files from within your header, but you'll get a compile error 
if the header file is included in more than one place.
If say a.cpp and b.cpp include c.h, the inclusion guard will make sure that it 
doesn't get included--the header body won't be read if the #ifndef example3_h 
evaluates to true, which is why we define it below the condition.
Microsoft compilers also support the #pragma once, but this does not allow for 
portability across compilers as far as i know.
HTH,
Thanks,
~~TheCreator~~
Visit TDS for quality software and website production
http://tysdomain.com
visit the piratecafe for programming related resources:
http://piratecafe.net
msn: [EMAIL PROTECTED]
skype: st8amnd127
  ----- Original Message ----- 
  From: Jos Timanta Tarigan 
  To: [email protected] 
  Sent: Wednesday, June 25, 2008 10:48 PM
  Subject: [c-prog] (beginner) .h and .cpp


  Hi folks,
  Im new to C++ and currently developing C++ under os x. 

  Im a little bit confused the difference between .h and .cpp. What is actually 
a .h file? What is the difference between .h and .cpp? I actually implement all 
my methods at .h file so my .h file look like this:

  #include <iostream>

  using namespace std;

  class Person
  {
  public:
  Person::Person(string input)
  {
  userName = input;
  }

  void Person::setFirstName(string input)
  {
  firstName = input;
  }
  private:
  string userName;
  };

  Is there any convention/trade off with this method?

  Thank you 

  Regards

  Jos Timanta Tarigan



   

  __________ NOD32 3219 (20080625) Information __________

  This message was checked by NOD32 antivirus system.
  http://www.eset.com


[Non-text portions of this message have been removed]

Reply via email to