Skip to main content
gamedev.net
🔒 Locked

classes

Started by Joker2000 Jul 5, 2000 at 9:51 PM 3 replies 350+ views
Original Post
Joker2000
Joker2000
I''m writing a program that uses a class "player" to hold information about the current player. Here''s a little snippet of my code: class player { public: player(string, int, int, int, int, int); ~player(void); void show_player_stats(void); private: string player_name; int weapon_status; int health; int armor; int money; int experience; }; void main(void) { create_new_player(); } void create_new_player(void) { string name; cout << "What is the name of your new player?"; cin >> name; player p1(name,0,100,0,500,0); p1.show_player_stats(); } Ok, it works fine when I use p1.show_player_stats() in the create_new_player() function, because that''s where I created p1. But, other functions need to use p1 also. For example, on the main menu screen for the program, the user should be able to press "S" for the player stats - which would mean calling p1.show_player_stats(). But because p1 is private to create_new_player(), this doesn''t work and therefore I can''t call p1.show_player_stats() from my main_menu() function. Now that I''ve told you what I CAN''T do, can anyone tell me how it CAN work? Is this where I''d have to make p1 global? How would I do that? If making it global isn''t the solution, what is?
Pexi
Pexi
This forum is about Delphi game programming.
You should declare p1 public so that it is shown to all.

Pekka Heikura
Pekka Heikurapekka.heikura@mbnet.fi
Chagarou
Chagarou
Sorry Pexi but this forum is about "Game development with Borland/Inprise compilers" not just about Delphi, C++Builder is a Borland compiler.

Joker2000 if you want return P1 you can do like this:

player* create_new_player(void)
{
string name;
cout << "What is the name of your new player?";
cin >> name;
player* p1 =new player(name,0,100,0,500,0);
p1->show_player_stats();
return(p1);
}

In the main function:

player* player1 = create_new_player();
...
player1->show_player_stats();
...
// don''t forget to delete player1
delete player1;
turbo
turbo
It''s nice to see some people with C++ Builder questions and answers...



[ turbo | turbo.gamedev.net ]
[ Michael Wilson | turbo sys-op | turbo.gamedev.net ]
Pexi
Pexi
Sorry, about that.
Pekka Heikurapekka.heikura@mbnet.fi

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.