Tuesday, 29 May 2012

Hi friends,
Today i am discussing here how to create user defined funcion in c++ by creating header files.


as we all know language defined header file contains built in funcion definition taht we need in our program in order to run some of our program code specified data structure like cout,cin etc. are all function that defined by language specification and  we use in our program by just including  statement "#include <iostream.h>" that include the input and output specific function in our program.

Just like that we can define function as we requuired and store it in .h files by then just include that .h file in any number of program we can use that function without defining again again.

Here in my example i create to header files swap.h and power.h, then include it in one program to see how it all works.

Below is the code for swap.h header files.


#include <iostream.h>




void swap(int a, int b)
{
int t;
t=a;
a=b;
b=t;
cout<<"Swaped values of a and b are :"<<a<<" "<<b;
}


Just save it in bin directory where usually you save your .cpp files
TO download swap.h file directly click below:

Now below is the code for power.h file:

#include <iostream.h>
#include <math.h>

void power(int a, int b)
{
int res;
res=pow(a,b);
cout<<"Result is :"<<res;
}

TO download power.h file directly click below:

Now after including this files in any of c++ program in which you want you use swap and power function you can simply use it.

I have use it in one of my sample program to just check it below image show's the output of program header.cpp:
So that's it however if you want to download above header.cpp click on below link:

So i hope yu have enjoy it!

for any kind of help contact me on my email: hi0001234d@gmail.com.


No comments:

Post a Comment