why is h file needed?

Discussion in 'C++' started by Montezuma's Daughter, Jan 8, 2008.

  1. Hi All
    I was wondering why h file ia needed?
    why can't everything be written in C file?
    thanks
     
    Montezuma's Daughter, Jan 8, 2008
    #1
    1. Advertising

  2. Montezuma's Daughter wrote:
    > I was wondering why h file ia needed?
    > why can't everything be written in C file?


    It can, and it does, in a way. In most cases the existence
    of headers (the 'h' stands for 'header') is just to create
    convenience. For example, if you want to use your class in
    more than one .C file (.C stands for the Unix file extension
    commonly used for C++ source file, not C language), you need
    the definition of that class to be visible to the compiler
    when it compiles a.C and b.C (and all other files that are
    compiled separately in your project). You can duplicate the
    code in each file, or you can simply place the code in some
    other file, call it 'myclass.h' and perform source code
    inclusion using the preprocessor directive #include. In
    that case, if you need to edit your class, you do it in the
    header, one place, instead of going through all definitions
    of the same class in all .C files.

    Or did I misinterpret your confusion?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 8, 2008
    #2
    1. Advertising

  3. Montezuma's Daughter

    Salt_Peter Guest

    On Jan 8, 1:13 pm, "Montezuma's Daughter" <>
    wrote:
    > Hi All
    > I was wondering why h file ia needed?
    > why can't everything be written in C file?
    > thanks


    We don't disscuss C files, source files are typically *.cpp files in C+
    +

    Its not needed, depending on what you do. Headers are used to keep
    seperate declarations isolated. Thats what *.h files do. If you choose
    to declare everything in your source files (ie: *.cpp) then don't be
    surprised when you realize that you can't find your declarations in a
    mess or reuse the same declarations in multiple translation units.

    Should i require a given type in 10 different projects and i already
    have a custom type that does the job, i'ld be nuts to rewrite the
    types, just copy type.h and type.cpp over. #include "type.h" (done)

    So its really a question of whether you want to do it the hard way, or
    the easy way. I pprefer the latter.
     
    Salt_Peter, Jan 8, 2008
    #3
  4. Montezuma's Daughter

    Pete Becker Guest

    On 2008-01-08 14:15:06 -0500, Salt_Peter <> said:

    > On Jan 8, 1:13 pm, "Montezuma's Daughter" <>
    > wrote:
    >> Hi All
    >> I was wondering why h file ia needed?
    >> why can't everything be written in C file?
    >> thanks

    >
    > We don't disscuss C files, source files are typically *.cpp files in C+
    > +
    >


    Funny thing, many compilers treat .C files as C++.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Jan 8, 2008
    #4
  5. Montezuma's Daughter

    Salt_Peter Guest

    On Jan 8, 2:20 pm, Pete Becker <> wrote:
    > On 2008-01-08 14:15:06 -0500, Salt_Peter <> said:
    >
    > > On Jan 8, 1:13 pm, "Montezuma's Daughter" <>
    > > wrote:
    > >> Hi All
    > >> I was wondering why h file ia needed?
    > >> why can't everything be written in C file?
    > >> thanks

    >
    > > We don't disscuss C files, source files are typically *.cpp files in C+
    > > +

    >
    > Funny thing, many compilers treat .C files as C++.


    they do, i didn't say .C files

    >
    > --
    > Pete
    > Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    > Standard C++ Library Extensions: a Tutorial and Reference
    > (www.petebecker.com/tr1book)
     
    Salt_Peter, Jan 8, 2008
    #5
  6. Montezuma's Daughter

    Pete Becker Guest

    On 2008-01-08 14:38:00 -0500, Salt_Peter <> said:

    > On Jan 8, 2:20 pm, Pete Becker <> wrote:
    >> On 2008-01-08 14:15:06 -0500, Salt_Peter <> said:
    >>
    >>> On Jan 8, 1:13 pm, "Montezuma's Daughter" <>
    >>> wrote:
    >>>> Hi All
    >>>> I was wondering why h file ia needed?
    >>>> why can't everything be written in C file?
    >>>> thanks

    >>
    >>> We don't disscuss C files, source files are typically *.cpp files in C+
    >>> +

    >>
    >> Funny thing, many compilers treat .C files as C++.

    >
    > they do, i didn't say .C files
    >


    I didn't say that you did. I merely hinted that maybe you were focusing
    on the wrong aspect of the orginal question.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Jan 8, 2008
    #6
  7. Montezuma's Daughter

    Jim Langston Guest

    Montezuma's Daughter wrote:
    > Hi All
    > I was wondering why h file ia needed?
    > why can't everything be written in C file?
    > thanks


    ..h files are not needed, they are desired. Consider a program that includes
    two different .cpp or .c files. This will normally create two object files.
    Consider that one of the source files declares some functions.

    int foo( int x ) { /*...*/ };
    char* bar() { /* ... */ };

    and such. Now, without a header file you will need to declare the
    prototypes in the other source file.

    int foo( int x );
    char* bar();

    There can be many functions, structures and the like and you would have to
    check the source file and copy lines for each one you wanted to call. This
    is where a header file comes in. A header file is basically just a list of
    prototypes and structures used in some object file (or library) that you can
    include in your source file without having to type them in manually each
    time.

    If you are only using one source file and no others, you could get away with
    not including header files for your code, but would probably still need to
    include header files for the system files, stdio.h, memory.h and the like.
    These are prototypes and such for system/core functions.

    --
    Jim Langston
     
    Jim Langston, Jan 8, 2008
    #7
  8. "Jim Langston" <> writes:

    > Montezuma's Daughter wrote:
    >> Hi All
    >> I was wondering why h file ia needed?
    >> why can't everything be written in C file?
    >> thanks

    >
    > .h files are not needed, they are desired. Consider a program that includes
    > two different .cpp or .c files. This will normally create two object files.
    > Consider that one of the source files declares some functions.
    >
    > int foo( int x ) { /*...*/ };
    > char* bar() { /* ... */ };
    >
    > and such. Now, without a header file you will need to declare the
    > prototypes in the other source file.
    >
    > int foo( int x );
    > char* bar();
    >
    > There can be many functions, structures and the like and you would have to
    > check the source file and copy lines for each one you wanted to call. This
    > is where a header file comes in. A header file is basically just a list of
    > prototypes and structures used in some object file (or library) that you can
    > include in your source file without having to type them in manually each
    > time.
    >
    > If you are only using one source file and no others, you could get away with
    > not including header files for your code, but would probably still need to
    > include header files for the system files, stdio.h, memory.h and the like.
    > These are prototypes and such for system/core functions.


    Well, with some IDE, you could have your sources in a database, where
    no source or header file would be defined really. You could then
    just define the compilation units (or let the IDE do it for you), and
    the IDE would generate a single source file for each compilation unit,
    containing all the declarations and definitions needed.

    --
    __Pascal Bourguignon__ http://www.informatimago.com/
    You're always typing.
    Well, let's see you ignore my
    sitting on your hands.
     
    Pascal Bourguignon, Jan 8, 2008
    #8
  9. Montezuma's Daughter

    Lars Uffmann Guest

    Pete Becker wrote:
    >>>>> why can't everything be written in C file?
    >>>> We don't disscuss C files, source files are typically *.cpp files in C+
    >>> Funny thing, many compilers treat .C files as C++.

    >> they do, i didn't say .C files

    > I didn't say that you did. I merely hinted that maybe you were focusing
    > on the wrong aspect of the orginal question.


    He got you there, Salt_Peter :)
     
    Lars Uffmann, Jan 9, 2008
    #9
  10. Montezuma's Daughter

    James Kanze Guest

    On Jan 8, 8:20 pm, Pete Becker <> wrote:
    > On 2008-01-08 14:15:06 -0500, Salt_Peter <> said:


    > > On Jan 8, 1:13 pm, "Montezuma's Daughter" <>
    > > wrote:


    > >> I was wondering why h file ia needed?
    > >> why can't everything be written in C file?
    > >> thanks


    > > We don't disscuss C files, source files are typically *.cpp
    > > files in C++


    > Funny thing, many compilers treat .C files as C++.


    I can remember when .C was the only extension used for C++. Of
    course, as soon as C++ was ported to MS-DOS, that changed:).
    The result is that there really isn't a standard---.cc seems to
    be the most widespread convention, at least at my customers, but
    .cpp is also common, particular in the Windows world (although
    I've never seen it on code not ported to Windows).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jan 9, 2008
    #10
  11. Montezuma's Daughter

    James Kanze Guest

    On Jan 8, 10:34 pm, Pascal Bourguignon <> wrote:
    > "Jim Langston" <> writes:
    > > Montezuma's Daughter wrote:


    > >> I was wondering why h file ia needed?
    > >> why can't everything be written in C file?
    > >> thanks


    > > .h files are not needed, they are desired. Consider a
    > > program that includes two different .cpp or .c files. This
    > > will normally create two object files. Consider that one of
    > > the source files declares some functions.


    > > int foo( int x ) { /*...*/ };
    > > char* bar() { /* ... */ };


    > > and such. Now, without a header file you will need to
    > > declare the prototypes in the other source file.


    > > int foo( int x );
    > > char* bar();


    > > There can be many functions, structures and the like and you
    > > would have to check the source file and copy lines for each
    > > one you wanted to call. This is where a header file comes
    > > in. A header file is basically just a list of prototypes
    > > and structures used in some object file (or library) that
    > > you can include in your source file without having to type
    > > them in manually each time.


    > > If you are only using one source file and no others, you
    > > could get away with not including header files for your
    > > code, but would probably still need to include header files
    > > for the system files, stdio.h, memory.h and the like. These
    > > are prototypes and such for system/core functions.


    > Well, with some IDE, you could have your sources in a
    > database, where no source or header file would be defined
    > really. You could then just define the compilation units (or
    > let the IDE do it for you), and the IDE would generate a
    > single source file for each compilation unit, containing all
    > the declarations and definitions needed.


    You're doubtlessly thinking of Visual Age.

    The standard does require "translation units" (although nothing
    says that you have to be able to compile them separately), and
    it requires that the declaration of anything used be present in
    the translation unit---from what I've heard, visual age wasn't
    conform in that regard (but there may have been an option to
    make it conform).

    More to the point, of course, on a large project, you want to
    keep your interface specification in a separate file from the
    implementation to begin with.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jan 9, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thomas Nick
    Replies:
    0
    Views:
    1,974
    Thomas Nick
    Jun 13, 2005
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    982
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,228
    Smokey Grindel
    Dec 2, 2006
  4. Ryan Macy

    Ideas needed & help needed!

    Ryan Macy, Jul 19, 2006, in forum: Ruby
    Replies:
    2
    Views:
    543
    Ryan Macy
    Jul 19, 2006
  5. John Farold
    Replies:
    0
    Views:
    408
    John Farold
    Aug 29, 2012
Loading...

Share This Page