How does this work-
I #include<cmath>
and call pow(x,y)
and it works. Now I create my own header file-
#ifndef HEADER_H
#define HEADER_H
void fun();
#endif
Its implementation is stored in header.cpp file. This is main.cpp where I will call fun()
-
#include "header.h"
int main(){
fun();
return 0;
}
And now if I compiled and linked my file like this-
g++ -Wall main.cpp -o Main
Then this will not work because implementation(header.cpp) of fun()
is missing.
For the fun()
function to work I need to do this-
g++ -Wall header.cpp main.cpp -o Main
I am told that header files are supposed to store only the interface. (Why?)
So, <cmath>
being a header should also contain only declaration of pow(x,y) function. But when I call pow(x,y) then it compiles without error and works fine. (How?)
To summarise, I have 3 questions-
1.Why are header files supposed to contain only interface? What bad can happen if I included interface and implementation in the same file?
2.How is built-in <cmath>
different from a user-defined "header.h" ?
3.Why not directly include header.cpp in main.cpp?