Improve company productivity with a Business Account.Sign Up

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Have some questions about .h files.

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!!!
3 Solutions
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.
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 )

>>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 :-? )

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.
morganftdAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now