Solved

Have some questions about .h files.

Posted on 2004-08-16
5
252 Views
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!!!
GM
0
Comment
Question by:morganftd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 13

Assisted Solution

by:SteH
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.
0
 
LVL 11

Expert Comment

by:avizit
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 )
 

0
 
LVL 11

Accepted Solution

by:
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 ..so 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 http://tinyurl.com/7yezw for further info
/abhiijt/

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

0
 
LVL 5

Assisted Solution

by:TrekkyLeaper
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.
0
 

Author Comment

by:morganftd
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 268
Lambda for random numbers problem 7 120
Focus not getting shifted out of  editbox 2 69
Which Linux flavors will this run on? 6 102
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

749 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