tags:

views:

83

answers:

1

i have following question is there possibility in c++ create header files using following rules let's take header

#include "sum.h">  

where in sum.h i will declare functions or procedure and then use it in .cpp files? thanks

+5  A: 

As far as I can tell, you want to declare functions in headers and then include the header somewhere and use it. This is normal use for headers. (You should get a book that explains this, since scrapping for information online isn't going to get you a very good knowledge-base.)

You can go about it in two ways. Declare the functions in the header, then define them in some translation unit and link with that translation unit:

// sum.h
int do_sum(int x, int y); // declare

// sum.cpp
#include "sum.h" // get declarations (strictly not needed)

int do_sum(int x, int y) // define
{
    return x + y;
}

The other way is to use inline to avoid the One-Definition Rule (ODR) and define the functions in the header:

inline int do_sum(int x, int y) // define
{
    return x + y;
}

Without inline, you'd get an error for having multiple definitions of the function.

GMan
A more generous answer than your "What?" above, but not as funny :-).
Tony