Hi.
I was wondering: when I create a new class in C++, it's generally said that it should go in a separate file.
So, in order to achieve that, you create a separate .cpp file and a header file that's supposed to hold the class prototype.
So, why not just use a .h file for the classes and be done away with it? Why do I also need a .cpp file?
It basically makes the code look a lot cleaner, especially when the classes get large, but also if you are using static variables inside your class, they must be declared in a .cpp file, since you can't compile a header file.
So, why not just use a .h file for the classes and be done away with it? Why do I also need a .cpp file?
.h file will be included i.e. copied to all files using this class. And you cannot have same function defined twice. That why you should move function definition to the .cpp file.