Hey guys i'm making my first game in c++ and I was wondering how to do
class initialization and structuring..
If you don't know how to write and design classes in C++, you're not ready to make a game of any complexity. I'm not passing judgement on your character or competence, I'm saying that simply for the same reason why you can't write novels in English if you can't form English paragraphs.
Go pick up a good introductory C++ book and learn from that first. Do the exercises, and take in the language one bit at a time.
I'm being dead serious. If you don't learn the C++ language properly before doing game programming you will experience absolutely nothing but frustration and difficulty. Please don't do that to yourself.
Game programming on its own is hard enough. Learning the fundamentals of a language on its own is hard enough. I know this from experience; I've tutored lots of students taking their first C++ class at the university I study at. You certainly don't want to do both at the same time.
for example I want to have a class called 'player' which handles all
player related tasks and I want to initialize it from my main class
called 'game'. I read over a few pages and come up with this for the
class structure, is it correct?
The correct syntax for a class looks like this:
class player
{
public:
player() { /* Constructor Definition */ }
}; // Note semicolon!
There is, however, much more to writing classes that work than just the above example. A good introductory C++ book will cover this and more.
If so how do I initialize this class from another class?
You don't initialize C++ classes, you instantiate them. Assuming you have a class called game
, you would instantiate an instance of player
called p
like this:
class game
{
private:
player p;
};
Then you would have a main
function that instantiates an instance of game
like this:
int main() // Note: void main() is not a valid main function in C++!
{
game g;
// ...
return 0;
}
Again, I highly recommend that you pick up a good introductory C++ book and learn from it. Don't bite off more than you can chew.