Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

give answer

why it is neccessry to include header files in c++?
0
sajid
Asked:
sajid
  • 6
  • 3
  • 2
  • +6
1 Solution
 
AxterCommented:
Is this homework?
0
 
atifmehdiCommented:
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
 
glebspyCommented:
It's not necessary.

It's done to save time.
0
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.

 
graham_kCommented:
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
 
jhanceCommented:
>>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
 
mnashadkaCommented:
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
 
graham_kCommented:
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
 
dash420Commented:
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
 
graham_kCommented:
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
 
garbouaCommented:
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
 
scientist060700Commented:
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
 
graham_kCommented:
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
 
scientist060700Commented:
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
 
graham_kCommented:
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
 
garbouaCommented:
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
 
scientist060700Commented:
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
 
graham_kCommented:
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
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.

  • 6
  • 3
  • 2
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now