Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > why is h file needed?

Reply
Thread Tools

why is h file needed?

 
 
Montezuma's Daughter
Guest
Posts: n/a
 
      01-08-2008
Hi All
I was wondering why h file ia needed?
why can't everything be written in C file?
thanks
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-08-2008
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


 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      01-08-2008
On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
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.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-08-2008
On 2008-01-08 14:15:06 -0500, Salt_Peter <> said:

> On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
> 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)

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      01-08-2008
On Jan 8, 2:20 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-01-08 14:15:06 -0500, Salt_Peter <pj_h...@yahoo.com> said:
>
> > On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
> > 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)


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      01-08-2008
On 2008-01-08 14:38:00 -0500, Salt_Peter <> said:

> On Jan 8, 2:20 pm, Pete Becker <p...@versatilecoding.com> wrote:
>> On 2008-01-08 14:15:06 -0500, Salt_Peter <pj_h...@yahoo.com> said:
>>
>>> On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
>>> 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)

 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-08-2008
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



 
Reply With Quote
 
Pascal Bourguignon
Guest
Posts: n/a
 
      01-08-2008
"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.
 
Reply With Quote
 
Lars Uffmann
Guest
Posts: n/a
 
      01-09-2008
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      01-09-2008
On Jan 8, 8:20 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-01-08 14:15:06 -0500, Salt_Peter <pj_h...@yahoo.com> said:


> > On Jan 8, 1:13 pm, "Montezuma's Daughter" <Urania.m...@gmail.com>
> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57