Regarding .h and .cpp files.

So I've been taught to never include .cpp files in the main.cpp but instead include the .h that references that .cpp file. This has been fine as my standard .h file looks something like this:

1
2
3
4
5
6
#ifndef SCREENTEXT_H
#define SCREENTEXT_H

void screenText();

#endif  


and the screenText.cpp file holds the actual function for screenText(). In my main I would include screenText.h

Now for the question. I want to divide my player class into a separate file as to keep it nice and organized from the rest of the code (main.cpp). I have a playerClass.cpp file where I have created my class. Sticking to what I've been doing I shouldnt just #include "playerClass.cpp" on my main, but instead I should include a header file that references the playerClass.cpp, right? What would my header(.h) file look like then? As previous stated I usually just declare the function in the .h file, but not sure what to do now that its a class.
Go have a read: http://www.cplusplus.com/forum/articles/10627/

Check section 4; it has an example of a class header file.
Excellent Thanks. I'll try to do a better job of searching around next time. Cheers.
I'd like to add something to my question now that I've had some time to fiddle around. In Visual Studio I used the "Add Class" feature so it generates both the .cpp and .h

I am not sure what it's doing exactly, maybe I need more practice with classes but this is what it's given me.

player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include "player.h"


player::player()
{
}


player::~player()
{
}


player.h
1
2
3
4
5
6
7
8
9
#pragma once
class player
{
public:
	player();
	~player();
};



Not exactly sure what the ~player(); and player(); is, or what the difference between the two are.
Last edited on
player() is the constructor; it is called when an object of the class is created.
~player() is the destructor; it is called when an object of the class is deleted/deallocated.
Topic archived. No new replies allowed.