Class Help

Hello,

I am currently a programming student, and i have a question that is similar to nested class's.


I want to have a few class's that are coding wise independent fro each other, BUT will link to each other.

For Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class CGame
{
private:

CPlayer* Player;

// Reset of code
}

class CPlayer
{
private:

// Rest of code
}


int main(void)
{
// Create Game
CGame* game = game -> fnCreateGame();

return 0;
}


please ignore the syntax error's, i not doing it for the code corrects.

I want to kind of use the Game class to control other class's, but no idea to do it and to create the class's without crashing.

Thank you.
closed account (zb0S216C)
Crazy Computing Connor wrote:
"I want to kind of use the Game class to control other class's"

Can you be more specific?

Wazzak
Since you didn't post fnCreateGame, we don't know the details of what it does, so it's hard to advise you.

As far as what you posted, line 21 is going to cause a crash.
You're trying to call fnCreateGame via an uninitialized pointer to game.

I think what you really want is:
 
CGame* game = new CGame;

Your constructor for CGame should do any necessary initialization.


Do you mean something like this: ?
Engine.h
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
#include "gameBoard.h"
#include "Shapes.h"
#include "GUI.h"

#include <vector>

struct key
{
	//... code ...//
};

class Engine
{
	public:
		Engine(void);
		~Engine(void);

		void run();
		void update();

		void kbHit( char &k );

		bool getKeyPressed( char &k );

		int randomRange( int _Min, int _Max );

		GUI gui;

	private:
		int gameSpeed;

		double startTime;
		double currentTime;

		Shapes *shape;
		gameBoard *board;
};


Engine.cpp
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
67
68
69
70
#include "Engine.h"

#include <Windows.h>
#include <conio.h>
#include <time.h>

Engine::Engine(void)
{
	srand( unsigned( time( NULL ) ) );

	shape = new Shapes( randomRange( 0, 6 ), 40, 3, UP );
	board = new gameBoard;

	gameSpeed = 200;
	startTime = GetTickCount();
}

Engine::~Engine(void)
{
	delete board;

	delete shape;
}

void Engine::update()
{
	if( shape->canMove( DOWN, board ) )
		shape->move( DOWN );
	else
	{
		for( int i = 0; i < 4; i++ )
			board->updateBoard( shape->getShapeOffset( i ), shape->getShapeType() );

		board->drawBoard();

		delete shape;

		shape = new Shapes( randomRange( 0, 6 ), 40, 3, UP );
	}
}

void Engine::run()
{
	char key = ' ';
	int i = 0;

	gui.startScreen();

	/*gui.gameSetup();
	return;*/

	gui.drawGameGui();

	while( true )
	{
		currentTime = GetTickCount() - startTime;

		if( currentTime > gameSpeed )
		{
			update();

			startTime = GetTickCount();
		}

		kbHit( key );

		if( ( getKeyPressed( key ) ) && ( key == 'q' ) )
			break;
	}
}


I create an object of Engine in main(), creating objects of the Shapes and board classes. Then call Engine.run(), creating a GUI object.

Everything is handled by either run() or update() within the Engine class, but each class could have an object created in main() and tested etc.

Any new'ed stuff is then deleted in the ~Engine destructor.

Edit:
This is what it does( So far ). And the Engine class handles all other classes.
Each class in-turn handles what it does.
http://www.youtube.com/watch?v=sNR8HejYJ6s
Last edited on
Topic archived. No new replies allowed.