i have mammal.h file reads:
#ifndef mammal_h #define mammal_h class mammal { public: void speak(); }; #endif
my cpp file looks like:
#include "stdafx.h" #include "mammal.h" #include <iostream> void mammal::speak() { using namespace std; cout << "speaking"; }
and use of code seen here:
#include "stdafx.h" #include "mammal.h" int main() { mammal *mammal = new mammal(); mammal->speak(); }
however, could in header file:
#include "stdafx.h" #include <iostream> #ifndef mammal_h #define mammal_h class mammal { public: void speak() { using namespace std; cout << "speaking"; } }; #endif
i haven't defined preference...but see both work. there advantages or disadvantages either of these approaches?
try calling code more 1 place -- , therefore #including header in more 1 source file -- , you'll see problem in second approach. linker doesn't if have more 1 definition of same function, if they're identical.
Comments
Post a Comment