A question about classes (C++) [SOLVED]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

A question about classes (C++) [SOLVED]

Post by superLED »

Hello there!
I am currently programming a game engine, but I've come across some problems.

I have a main class (Game), that should take care of most of my stuff.
I also have another class (Player), that holds the values of the player and such.

But I have no idea how to use the Player class inside my Game class, so I can do like this:

Code: Select all

void Game::draw()
{
player.draw(player.x, player.y);
}
Can someone please help me with this?
I want the Game class to be able to handle most of the Player class' functions.
Last edited by superLED on Tue Jan 11, 2011 6:28 pm, edited 1 time in total.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: A question about classes (C++)

Post by dandymcgee »

Game.h

Code: Select all

#include "Player.h"

class Game {
	private:
		Player player1;
	public:
		Game();
		Update();
		Draw();
}
Game.cpp

Code: Select all

Game::Game() {
	player1.SetPosition(32, 128);
	player1.SetSprite("images/player1.png");
}

void Game::Update() {
	player1.Update();
}

void Game::Draw() {
	player1.Draw();
}
This is one common way to go about it. Simply declare a Player as a member variable as the Game class.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: A question about classes (C++)

Post by lotios611 »

Have your Game class hold a Player object, and have a setPlayer() method in the Game class.

Game class

Code: Select all

class Game
{
public:
//Whatever code
setPlayer(Player player);
private:
Player _player;
}
So now all you have to do is create a player object in your main.cpp or wherever, and than call the setPlayer() method.

Edit: Combine what I said with what dandymcgee said.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: A question about classes (C++)

Post by superLED »

dandymcgee wrote:Game.h

Code: Select all

#include "Player.h"

class Game {
	private:
		Player player1;
	public:
		Game();
		Update();
		Draw();
}
Game.cpp

Code: Select all

Game::Game() {
	player1.SetPosition(32, 128);
	player1.SetSprite("images/player1.png");
}

void Game::Update() {
	player1.Update();
}

void Game::Draw() {
	player1.Draw();
}
This is one common way to go about it. Simply declare a Player as a member variable as the Game class.
Oh my, exactly what I tried, but I didn't include the player.h in game.h.

Thank you so much! *Hands you a cake*
Last edited by superLED on Tue Jan 11, 2011 1:38 pm, edited 1 time in total.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: A question about classes (C++)

Post by dandymcgee »

superLED wrote: Oh my, exactly what I tried, but I didn't include the player.h in game.h.

Thank you so much! *Hans you a cake*
It was an honest mistake. ;)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: A question about classes (C++)

Post by N64vSNES »

dandymcgee wrote:
superLED wrote: Oh my, exactly what I tried, but I didn't include the player.h in game.h.

Thank you so much! *Hans you a cake*
It was an honest mistake. ;)
OMFG! You got cake, NOW I'm jealous :'(
Post Reply