I have the following question whether there is the possibility in c++ to create header files using the following rules. Let's take the header
#include "sum.h"
where in sum.h i will declare functions or procedures and then use it in .cpp files?
I have the following question whether there is the possibility in c++ to create header files using the following rules. Let's take the header
#include "sum.h"
where in sum.h i will declare functions or procedures and then use it in .cpp files?
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.