creating a class [solved]

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

creating a class [solved]

Post by Clodi » January 17th, 2013, 4:00 pm

Hello,
I was trying to make my game less messy by using classes.
I am learning classes now for the first time and I am not sure i get it so let me start off asking:

1) Is the possibility of "initialising all the objects at once" and "limiting user access to class members" the only reason why I should use classes rather than structures?

2) Assuming I am making a game where you've got a little man going around killing enemies, and assuming I want to make a class for these enemies, would it make sense to create another .h and .cpp files for this class?
Are ".h +.cpp" files only meant to improve organisation?

Also, this is my class:

Code: Select all

class Player
{
	float positionX;
	float positionY;
	float velocity;
	int colour;
	int health;
public:
	Player();
	~Player();
	void updatePosition();
	void updateSpeed();
	void updateColour();
	void updateHealth();
	bool checkCollision();
};
I want the actual parameters of my little dude being not accessible so that they will only be accessed by member functions belonging to Player class. PROBLEM, how to do this? In "game.cpp" every time I try to draw a shape controlled by my parameters, the compiler always stops me because of the obvious reason that it can't access private class members.

Code: Select all

gfx.DrawLine( 100,100,200,200,0,0,0 ); //this is fine

Player Mike;
gfx.DrawLine( Mike.x,Mike,y,200,200,0,0,0 ); //this is not
A way around this?

Also, my class has variables and functions and when I want to define a function in Player.h, there is absolutely no problem (see code at the beginning) BUT when I want to define a function in Player.cpp it turns out that I am not allowed to use "kbd.etc..","gfx.etc..".

Code: Select all

void Player::updatePosition()
{
	if ( kbd.DownIsPressed )
}
Why?
I attached everything:

Code: Select all

#include "D3DGraphics.h"
#include "Keyboard.h"
#include "Mouse.h"
#include "Sound.h"
Please help me :)
Last edited by Clodi on January 26th, 2013, 7:55 pm, edited 5 times in total.

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: I'd need some help with classes please

Post by Musi » January 17th, 2013, 4:54 pm

The way you would use private members of a class is make a public function that does what you want and then call that instead. So for your draw line problem, you could give your Player class a Draw(); function, this function would have access to Players private members.

As for why you cant use the kbd and gfx objects, that's because the Player class doesn't know what they are as they are created in the Game class. What i would do is pass the address of the objects to the Player classes constructor and store them.

Code: Select all

class Player
{
     D3DGraphics *pGfx;
     KeyboardClient *pKbd;
public:
     Player( D3DGraphics *gfx, KeyboardClient *kbd );
     ~Player();
};

// The constructor.
Player::Player( D3DGraphics *gfx, KeyboardClient *kbd )
:    pGfx( gfx ),
     pKbd( kbd )
{}
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: I'd need some help with classes please

Post by Clodi » January 17th, 2013, 7:01 pm

Roger that.

Thank you sooo much :)

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: I'd need some help with classes please [solved by Musi]

Post by Clodi » January 17th, 2013, 9:03 pm

Now how and where do I create an object? :(

Code: Select all

Player Charlie;
inside "composeframe" in game.cpp doesn't work. It says: no constructor exists for "Player" but it exists and the code runs perfectly unless I try to create objects with my class and constructor..

Also, inside Player.cpp I still cannot use pGfx and pKbd..

I have attached my work here. :roll: ..
Attachments
yo - Copia.rar
(42.72 KiB) Downloaded 132 times

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: I'd need some help with classes please

Post by Musi » January 17th, 2013, 9:58 pm

Do you mean it says "no default constructor"?

When you create a class object you need to pass it all the things that its constructor requires.

Code: Select all

Player charlie( &gfx, &kbd );
I think the best place to create your player would be as a private member of the Game class. Now i don't think you can initialize a class directly inside another class so what to do is first declare the Player inside the Game class with no arguments.

Code: Select all

class Game
{
private:
     Player charlie;
}
Then in Game's constructor you can initialize it.

Code: Select all

Game::Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer )
:    gfx( hWnd ),
     audio( hWnd ),
     kbd( kServer ),
     mouse( mServer ),
     charlie( &gfx, &kbd )
{}
If you want to create a class object without initializing it you just need to add a default constructor, which is a constructor with no arguments.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: I'd need some help with classes please

Post by Clodi » January 17th, 2013, 10:12 pm

Thank you so much Musi!!!
Yes "No default constructor" that's what it said!
I now got it to work.

How to use gfx. and kbd. though?
I have to take input from keyboard to work with my class and i can't because everytime I type "kbd." or "gfx." in "Player.h" I get no intellisense :(

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: I'd need some help with classes please

Post by Musi » January 17th, 2013, 11:35 pm

Since pKbd and pGfx are pointers to the objects, rather than the objects themselves, you need to use -> to access them.

Code: Select all

pGfx->DrawLine();
I named them differently than the actual objects to save confusion. :D
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: I'd need some help with classes please

Post by Clodi » January 18th, 2013, 8:16 am

thank you so much Musi :)

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: I'd need some help with classes please [solved by Musi]

Post by Clodi » January 18th, 2013, 5:22 pm

I can't believe I have been stuck on classes for so long and I still don get them too well, but I am sure it makes no sense to barge my way forward for more videos if I am not fine with classes. i think they maybe the most important thing to learn.

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: I'd need some help with classes please [solved by Musi]

Post by indus » January 18th, 2013, 7:47 pm

Classes are like pointers. Not something that you understand straight away after reading what they are and how are they being useful. The more you use them the more you understand them. Just dont give up. :D

Post Reply