0

Hey guys i'm making my first game in c++ and I was wondering how to do class initialization and structuring.. 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?

class player {


public:

  player():   


}

If so how do I initialize this class from another class?

Thanks for any help.

Duncan Palmer
  • 2,865
  • 11
  • 63
  • 91
  • 5
    Read some introductory book first. [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Nawaz Nov 20 '11 at 07:27
  • I don't have any books, that's why I am here. – Duncan Palmer Nov 20 '11 at 07:28
  • 1
    You can find tutorials / introductory materials on C++ via Google. This is a place for answers to fairly specific questions. Questions that require an entire book can't really be handled in this format. :) – Mordachai Nov 20 '11 at 07:29
  • 2
    @Duncan Palmer: Then go get [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – In silico Nov 20 '11 at 07:29
  • 1
    If you don't have books, then buy. Nobody will teach you things for which books are there. – Nawaz Nov 20 '11 at 07:29
  • 1
    On the subject of reading: please re-read the StackOverflow FAQ. We can help solve specific programming problems, but we do not provide basic language tuition. – johnsyweb Nov 20 '11 at 07:35

2 Answers2

2

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.

Community
  • 1
  • 1
In silico
  • 51,091
  • 10
  • 150
  • 143
1

This introduction to c++ classes might be helpful: Introduction to C++ Classes

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
Gaff
  • 88
  • 6