I am a beginner in C++. Look the following code:
// in myclass.h
class MyClass
{
public:
void foo();
int bar;
};
// in myclass.cpp
#include "myclass.h"
void MyClass::foo()
{
// does something
}
//in main.cpp
#include "myclass.h" // defines MyClass
int main()
{
MyClass a;
return 0;
}
as far as I understand, in order to make interfaces separated from implementations, the source code file main.cpp
includes the header file myclass.h
; the latter contains the attributes and the prototypes of the methods within class MyClass
, but not the implementation of those methods which are instead contained by myclass.cpp
. In turn myclass.cpp is
including myclass.h
.
Can someone explain me how exactly the compiler links the prototype contained in a header file to its actual implementation?
In this particular case, I am asking how myclass.h
is linked toward myclass.cpp
.