   .cpp versus .h
  I know that .cpp are c++ source code files and that .h defines a header file.  Note, however, that both files are written using c++ code.I was always told to include prototypes in a header file.Lately I have been linking extra files to my programs, like constants and classes ect,.  I have able to use both .h and .cpp extensions with no consequences.  Is there any protocol that I should be using to determine which extension to choose?Thank you   
  GDNet has an article about this topic.Class definitions go in the .h file. The functions of the class go in the .cpp file.Example://The .h file, we'll name it 'MyClass.h'//Used to make sure the class is only declared once.#ifndef MYCLASS_H#define MYCLASS_Hconst int myGlobalIntConstant = 5; //Constants can be declared in the header, and initialized at the same time.extern int myGlobalInt; //Variable pre-declaration. Must be labeled 'extern'.class MyClass //Class declaration.{    public:    MyClass(); //Constructor pre-declaration.    ~MyClass(); //Destructor pre-declaration.   void DoSomething(); //Function pre-declaration.   private:    int myClassVariable; //Variable declaration.};#endif//The .cpp file for that class.//Include the class' header file.#include "MyClass.h"int myGlobalInt = 357; //The global variable should be initialized here in the source file.//Basically, it's getting declared and initialized here, and in the header file it's being told//that it can be accessed externally by any source file that includes that header file.//If the variable doesn't need to get accessed by any files other than this one, you don't//need to put the pre-declaration in the header file.MyClass::MyClass() //Constructor declaration. (runs when the class is created){    myClassVariable = 0; //Variable initilization.}MyClass::~MyClass() //Destructor declaration. (runs when the class is destroyed){    }void MyClass::DoSomething() //Function declaration.{    //Do whatever...}In the C programming language, the source files were labeled .c, and their header files were labeled .h. In C++, the source files are labeled .cpp, and the header files are also labeled .h, but some people prefer to label the header files .hpp, to differentiate from C's header files. Doesn't really make any difference; they are all text files that include C++ code. It's only how you use them that makes a difference. They could all be named .txt and still work the same; although some compilers might gripe.   ;  You usually set it up like this for classes://This is myClass.h#include <iostream>class myClass {private:    int myVariable;    public:    myClass();    void doSomething();};//This is myClass.cpp#include "myClass.h"myClass::myClass() {    myVariable = 1;}void myClass::doSomething() {    myVariable++;    std::cout << myVariable << std::endl;}That is basically how I work with classes in header and cpp files. You use both for each classes: definitions go in the header, the 'actual' code in the cpp file.Here's an example of using variables in cpp & h files://This is myVariables.h#include <iostream>extern int someVariable;extern const int someConstantInt;extern const double someConstantDouble;//This is myVariables.cpp#include "myVariables.h"int someVariable = 10;int someConstantInt = 2;const double someConstantDouble = 2.15;Hope this helps.   ;  If you need to use the functions in other cpp files, you will want to use a header file to avoid code replication and then maintenance nightmares when something changes. If you are declaring a function that you only use in the current cpp file, then you do not have to use a header file and you can just place it at the top of the file.In addition, cpp/c files are compiled automatically by Visual Studio. You will get a compiled object file for each file you have. A h/hpp file is not compiled itself but rather pasted into the file that includes it (that is how the preprocessor works).You might also want to take a look at this GameDev article: Organizing Code Files in C and C++ . It's a bit dated now, but it should still be relevant.[edit]lol, I refreshed before I posted to make sure there were no replies and there were none. After I posted there are two.   ;  Just remember that only C/CPP files are compiled. Headers do not (directly) get compiled into object code.When a CPP file gets compiled, all of the headers that it includes (and the headers they include) are basically copy/pasted into the CPP file. The resulting huge CPP/Header hybrid is then compiled to object code.Once this makes sense to you, then all sorts of compile/link errors are common sense.e.g. if you write a function in a header (full body of the function, not just a prototype), and then that header is included by two CPP files, then the function will be compiled twice (and cause a link error).On the other hand, if you put the prototype in the header (and write the actual body in a single CPP file), then the function only gets compiled once, but multiple CPP files know how to call it (from the prototype).Hope that helps you understand the difference ;)   