Have some questions about .h files.

Posted on 2004-08-16
Last Modified: 2010-08-05
Hi I'm semi-new at C++ and need some clearification. I understand that the standard library has a corresponding header file that contatins the function prototypes for all the functions in that library.

But why would I need to create my own if there are standard ones already out there?
Is it just that the more complicated your source code gets you might need to write your own .h file?

Why does it have to be a seperate file? Can't I just put that information in my .cpp file?

Can you post some small or general examples of what a .h file you'd create on your own would contain?

Thank you!!!
Question by:morganftd
LVL 13

Assisted Solution

SteH earned 25 total points
ID: 11810781
Normally you create for each class a .h and a .cpp file. The first contains the prototypes for the class and all functions. It must be included in all .cpp files using an object of this class. The .cpp file contains the actual implementation of the functions. It is needed only once in your project.

If you write a more C-ish file the header contains the function prototypes and the cpp file the implementation. Rest as above.
LVL 11

Expert Comment

ID: 11811434
correct me if I am wrong

what I understood is that : you want to say why include the standard library headers if they are standard only

if that is so . the reason is .. there are many standard library  as in the maths library , the standard input /output library etc
now in your application you may not use any mathematical function like sqrt() so in that case you dont include the math.h file
so yuor final executable doesnt contain the code for sqrt()
now if all standard libraries were included automatically then yuor execuatble size would be large

including a particular header file tells the compiler exactly what we want ....

(the above explanation is in C-speak .. but i am sure ut applied to the C++ world too )

LVL 11

Accepted Solution

avizit earned 100 total points
ID: 11811526
>>But why would I need to create my own if there are standard ones already out there?

um let's say you have a complicated function func1(int x , int y , int z, float f )....

now this function is not a standard funtion you have to write your own . Now if there are many such functions you surely don't want to put all the code in one file along with your main() .

to make it more understandable you write all your function in one or many files say file1.c file2.c
and put the function protoypes from each files ( and other stuff) into corressponding header files fil1.h file2.h etc etc

so now in your file containing the main() you just need to include the files file1.h and file2.h depending on your needs ..
so it basically serves two purpose

1. easy code maintablity
2. code resue ( you dont have to write the whole function (i,.e define ) in every file that the function is used. You juse define at one place and include the header wherever the function is used

read for further info

( something tells me I have misunderstood the question all along :-? )


Assisted Solution

TrekkyLeaper earned 25 total points
ID: 11813037
In addition, header files give you a level of security if you are writing a library. If you had all your functionality and declarations in one file, that file would have to be distributed. If you use a header file, you can compile your library and distribute the binary library with the header files. That way, people get all the information they need to use your library (from the header file) and do not get to see any of your actual implementation.

Author Comment

ID: 11813775
Thanks everyone! What you all wrote together really made sense. Avizit you did'nt misunderstand what I was asking, in fact you made it click. I think I got it.

TrekkyLeaper I liked that point about people getting all the info they need from my library without seeing any of it, and in a way it's more "secure" in it's own file rather than in the main().

I know one day I will look back and say "Why didn't I get that, it was so easy?", but if you don't ask you don't learn right? Thanks all!!! I split up the points for this.

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question