Regarding .h and .cpp files.

Dec 16, 2014 at 11:36pm
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.
Dec 16, 2014 at 11:41pm
Go have a read: http://www.cplusplus.com/forum/articles/10627/

Check section 4; it has an example of a class header file.
Dec 16, 2014 at 11:49pm
Dec 17, 2014 at 12:12am
Excellent Thanks. I'll try to do a better job of searching around next time. Cheers.
Dec 17, 2014 at 2:52am
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 Dec 17, 2014 at 2:53am
Dec 17, 2014 at 6:16am
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.