i want to make a predefined function of mine own. plz tell me which header file to modify and how. i am a beginner here so plz be a bit more specific.i use turbo and borland compilers.
You simply create a file with a ".h" extension such as "myFunctions.h" .
In that file you put the function prototype
EG
int myAddFunction(int,int);
You then create a second file with a ".cpp" extension ("myFunctions.cpp") and put the body of the function in that
EH
1 2 3 4 5
#include "myFunctions.h" //Note "" not <> as is your library not system
int myAddFunction(int a,int b)
{
return a+b;
}
Then in any file you need to call myAddFinction() simply put
#include "myFunctions.h"
at the top with your other #includes.
You can have as many functions or classes as you like declared in the .h file. Note that this should only have functions in it that you want to be available to other blocks of code. You should try to group functions in some logical structure when you do this.
Just out of curiosity, why would you make two files? why not just name the .cpp a .h file and save the extra space? Unless this is compiler specific. I use the VC++ Express compiler, however I use Notepad++ for actual code editing. Anywho point was I would think it would be more efficient to just use one file instead of two.
If you implement a function in a file that gets #included directly by, say, 5 other .cpp files, then when the compiler compiles those 5 .cpp files into .o(bj) files, each object file gets a complete copy of the compiled version of that file. Which will lead to two problems -- first, when you go to link all the .objs into an executable, the same symbol will be defined in every .obj file causing linker errors, and second, why have multiple copies of the same compiled code in the application at all (it just makes your application bigger)?