Advertisement

Need help with some practice code

Started by February 13, 2003 04:06 PM
7 comments, last by SSJCORY 21 years, 9 months ago
I wrote this sorta beggining for a game and I found out that when I use a constructor in a function it doesn''t work. Hold on it''ll be easier to understand if you see it.
  
#include<iostream.h>
#include<windows.h>
class CPlayer{
private:
	int level;
	int health;
	int maxhealth;
	int attackpower;
	int defensepower;
	int speed;
	int magicpower;
	int maxmagicpower;
public:
	CPlayer(int type){
		level = 1;
		if(type == 1){
			health = 100;
			maxhealth = 100;
			attackpower = 30;
			defensepower = 30;
			speed = 10;
			magicpower = 20;
			maxmagicpower = 20;
		}
		showstats();
	}
	void showstats(){
		cout<<"Level: "<<level<<"\n";
		cout<<"Health: "<<health<<"/"<<maxhealth<<"\n";
		cout<<"Attack Power: "<<attackpower<<"\n";
		cout<<"Defense Power: "<<defensepower<<"\n";
		cout<<"Speed: "<<speed<<"\n";
		cout<<"Magic Power: "<<magicpower<<"/"<<maxmagicpower<<"\n";
	}
};
createplayer(){
	int ptype = 1;
	if(ptype == 1){
		CPlayer player1(1);
	}
}
int main(){
	createplayer();
	player1.showstats();
	return 0;
}
  
The line player1.showstats(); doesn''t work when I create the player inside of the createplayer() function. Plz help. Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.| My website!
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
You''re CPlayer definition is local to the CreatePlayer() function and does not exist in main()
Disclaimer: "I am in no way qualified to present advice on any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)"
Advertisement
Is there a way to make it work?


Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.|
My website!
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
declare player1 outside of a function.. make it global
Disclaimer: "I am in no way qualified to present advice on any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)"
I plan to make many different types of players with the functions for example.
cout<<"What player type would you like to be?";
cin>>playertype;
if(playertype == 1){
CPlayer player1(1);
}
if(playertype == 2){
CPlayer player1(2);
}
and so on.

So inside the constructor will have:
if(type == 1){
health = 100;
attack = 100;
}
if(type == 2){
health = 80;
attack = 80;
}

that way i can do everything within the function.
Thanks, hope this iterates.


Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.|
My website!
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Hmm.. well.. one, you could write the class differently.. to be assigned a player type and then process the date based on it... if you are sure about doing it this way, i THINK you can dynamically allocate a CPlayer variable... by declaring

CPlayer *pointer;

and then..

pointer = new CPlayer(1);

you should then be able to access the members of the class by using pointer->MemberName... make sure you declare the pointer as global though.. i''m pretty sure that would work..
Disclaimer: "I am in no way qualified to present advice on any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)"
Advertisement
That sort of code is going to drive you insane once you reach a certain level of complexity. This is why you use polymorphism.
I agree but when I try using polymorphism I get access violations.


Favorite Quotes:Gandalf: You shall not pass!|Gollum: My Precious!|Smeagol: My little hobbitses.|
My website!
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
That can''t be good.

This topic is closed to new replies.

Advertisement