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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

831 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