Advertisement
TheWhiteFang

Tutorial 4 Section A (e) [extern function]

Dec 16th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. //http://pastebin.com/u/TheWhiteFang
  2. //this code contains 3 parts (mainstr.cpp,str.cpp,str.h)
  3.  
  4. -----------------------------------------------------------------------------------------------------------------------------------
  5. //str.cpp
  6. //declare string
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. string name = "AFK";
  12. -----------------------------------------------------------------------------------------------------------------------------------
  13.  
  14. //str.h
  15. #ifndef STR_H // header guards
  16. #define STR_H
  17. #include <string>
  18.  
  19.  
  20. using namespace std;
  21. // extern tells the compiler this variable is declared elsewhere
  22. extern string name;
  23.  
  24. #endif;
  25. -----------------------------------------------------------------------------------------------------------------------------------
  26. //mainstr.cpp
  27.  
  28. #include "str.h"
  29. #include <iostream>
  30.  
  31. int main()
  32. {
  33.     cout << name <<endl;
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement