Solved

Have some questions about .h files.

Posted on 2004-08-16
5
221 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 58
returning a dereferenced pts in C++ 10 130
C++ get user from AD  (VS6) 7 48
Best book to learn C++ 4 53
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now