Help splitting file into header, cpp files

I'm a c++ student and we made a class file and now we have to split the file into a header file, a main file, and the file itself, in my case it would be asteroids.cpp, main.cpp, and asteroids.h

Getting to the point, can someone show me how to split my code into the three files and how/why the files are split up (specifically the two cpp files)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <ctime>
using namespace std;

class Asteroid
{
public:
	void displayMessage(int numCreated)
	{
		cout<<"There's an asteroid coming!"<<endl;
		cout<<"This is asteroid number: " << numCreated << endl;
		cout<<"It's approximately "<<asteroidSize<<" km in diameter!"<<endl;
		cout<<"It's moving at "<<asteroidSpeed<<" kmph!"<<endl;
		cout<<"\n";
	}
	void setSize(int size)
	{
		if (size < 1)
		{
			asteroidSize = 1;
		}
		else
		{
			asteroidSize = size;
		}
		if (size > 20)
		{
			asteroidSize = 20;
		}
		else 
		{
			asteroidSize = size;
		}
	}
	void setSpeed (int speed)
	{
		asteroidSpeed = speed;
	}

private: 
	int asteroidSize;
	int asteroidSpeed;

};


int main()
{	
	Asteroid firstAsteroid;
	Asteroid secondAsteroid;

	srand( (unsigned int) time(0) );
		
	firstAsteroid.setSize((rand() % 50));
	secondAsteroid.setSize((rand() % 50));
	firstAsteroid.setSpeed((rand() % 2000));
	secondAsteroid.setSpeed((rand() % 2000));
	firstAsteroid.displayMessage(1);
	secondAsteroid.displayMessage(2);

system("pause");

return 0;
}
A header file will have just the declarations of all class methods and members. The corresponding cpp file will have the actual definition of all these.
Topic archived. No new replies allowed.