Solved

give answer

Posted on 2001-06-14
17
188 Views
Last Modified: 2010-04-02
why it is neccessry to include header files in c++?
0
Comment
Question by:sajid
  • 6
  • 3
  • 2
  • +6
17 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 6193997
Is this homework?
0
 

Expert Comment

by:atifmehdi
ID: 6193999
otherwise u have to write the code of .h file in .cpp file

The thing is there should be something that should tell that there is a class with these many veriables and these many functions...

by including the .h file in .cpp , u actually tells the functions that there is a class with that name and with there variables and member functions
0
 
LVL 1

Expert Comment

by:glebspy
ID: 6194093
It's not necessary.

It's done to save time.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 6

Expert Comment

by:graham_k
ID: 6194120
it is necessary if more than one .cpp file wants to access the same information. including, but not limited to:

- function prototypes, where a.cpp uses a fn() declared in b.cpp
- same for class definitions
- shared constants, such as #defines and enums

Sure, in theory you can just duplicate this in all .cpp files which need it, but, but if something needs changing, you would have to change it in all. With a .h file, you only need to change it once.
0
 
LVL 32

Expert Comment

by:jhance
ID: 6194741
>>why it is neccessry to include header files in c++?

Who told you this was necessary?  If they've told you other things, suspect the information since they clearly don't know what they are talking about.

Header files are NOT required by a C++ program.
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 6194754
Also, with header files, you can allow users to utilize functions in static and dynamic libraries without distributing the code.  This is important if you sell libraries that perform a specific function (i.e. socket code, database access, etc.).
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6194886
jhance
> Header files are NOT required by a C++ program.


Strictly true, but what do we want to teach the guy here? Why is he asking?

Can we agree that they are syntactically unnecessary, but are A Good Thing?

In my line of work we have millions of line of code in hundreds, sometimes thousands of source files. In theory, we could put it all in one big file & take turns to edit it (if the editor could open a file that large).

Let's discuss the approach that good design/coding pracrise dictates the use of header files (and multiple sozrce files) (please?).
0
 
LVL 6

Expert Comment

by:dash420
ID: 6194922
It is something we don't like write again. as header file always contains function declaration and definition of constant variable.
otherwise we just write it in our cpp file. that is really boring. is not it.

0
 
LVL 6

Expert Comment

by:graham_k
ID: 6194923
On large projects, many developers work together. The work is split into managable portions (on a functional basis) & each takes one. The .h file contains his interface to the rest of the project.
0
 
LVL 5

Expert Comment

by:garboua
ID: 6195314
yes i agree about the importance of a header file, but it is not necessary or required. you can always use extern and tell the compiler to take it as is until linking time.  I don't know why someone would rather do that then have an interface file, a header file, but it is possible.
0
 
LVL 1

Accepted Solution

by:
scientist060700 earned 50 total points
ID: 6196855
sorry in advance for any repetation.

There is not always need for to include header file.  Microsoft put math,iostream, etc... into the .h file but visual c++ 6.0 does not require you to include header file.  You can write #include<iostream> or #include<cmath>.  Although sometime it may be necessary to include header file when you are trying to call your own created file to the seperate program so, that it can read from your another program.  When you try to link one class to another or one program to another you may need to call the header file of the different class/program so that it can read from other file but for microsoft visual c++ 6.0 not anymore requires you to write .h everytime you call the file
--scientist
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6199763
so why was that the correct answer, rather than previous postings which basically said the same?

Probably because 'scientist', who is very new around here, posted an answer, when it should have been a comment and sajid, who is also new and presumably doesn't know any better, decided that the easiest thing to do was to accept the answer.

sajid, I will think twice before wastig my time posting to any of your questions in future.

Axter, any comments ?  :-)
0
 
LVL 1

Expert Comment

by:scientist060700
ID: 6202494
If you knew better why did you post your comments as an answer?
And for your information I have been member since many years mostly in java.
--scientist
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6205265
hi scientist,

  nothing personal, just blowing off steam. There has been (yet another) on-going discussion recently as to what is the proper etiquette with regard to comments/answers - see
http://www.experts-exchange.com/jsp/qShow.jsp?ta=cprog&qid=20133039

with most experts agreeing with EE (see guidelines at teh foot of this, and every, page) that one should post comments and let the questionner decide which has helped him.

You know the reason as well as I do. As soon as a question is locked, far fewer experets will look at it (read 'almost none') and so the questionner is given lass help. Not answering also allows a debate between experts, thus explaining more of the thought process which is helpful both for the questionner & anyone who buys the question as a PAQ.

That's why I always post comments, never answers. Whether _I_ think I have a 'better' answer (than something that has not yet been posted?) is immaterial. The decision should be for the questionner.

Sorry that I thought that you were new here (by your quick answering). When I looked at your profile, I didn't notice the joining date, just that you had only answered 2 questions, so I took you as a newbie.

Hope that clears things up. Please don't be angry or offended; but, also, please don't be so quick on the answer button in future - it's best for EE; it's best all round.

best wishes,

Graham

0
 
LVL 5

Expert Comment

by:garboua
ID: 6206050
don't mean to get into the comment and answers.  MS VC++ does not require the include files BECAUSE, it bundled all these files under stfx whatever the name is. even though it is very helpful, it is NOT a good practice, because as most of you Don't know, windows is not the only most popular development platform, and it limits developers versatility to and the code to be protable, aaaaaaaaaaaaah, did someone said Software engineering 1-1????
0
 
LVL 1

Expert Comment

by:scientist060700
ID: 6208717
Hi Graham,
I didn't take it personnaly, It's just that I have been commenting many answers and no matter how best my comments were never got it accepted as answer so, I started answering few days ago and earned some points and I still think and re read my comments before i post them as answers.  Thanks for advice. I will pay more attention to it.
I hope to discuss with you on future questions.
--scientist
0
 
LVL 6

Expert Comment

by:graham_k
ID: 6209332
yes, that's the problem that was discussed on the thread above. Of course, if *everyone* posts as comments only, there is no problem. But, as soon as one or two post answers, others feel like you do, that they are losing points & start to post answers too & this becomes worse & worse. It's just human nature, I guess. Even top 15 guys have started doing it.

Personally, I don't really care about points (you can't buy beer with them), but like to try to help.

I will see you around on other questions.

best wishes,

Graham
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 101
Best book to learn C++ 4 78
operator overload: incompatible type and too many params 5 76
c++, dynamic object by json 1 41
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…
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. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

813 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

14 Experts available now in Live!

Get 1:1 Help Now