player and still be able to use methods. I have a CPlayer class and a Barbarian class and a Necromancer class. I want the person to be able to choose which type they want to be. I tried to use...
void main(){
int chartype;
cout<<"1 barbarian , 2 necromancer";
cout<<"Pick a type:";
cin>>chartype;
if(chartype == 1){
Barbarian player;
}
else{
Necromancer player;
}
But then when i want to use a method it gives me errors... Is there a way to make it so I can do this... Here''s the full source code.
//Copyright 2003 Cory Fisher
#include<iostream> //For cin,cout
#include<string> //For string use
#include<stdio.h> //For standard io
using namespace std; //Dont know why just everyone says you should
string nameSet(); //Forward declaration of nameSet()
class CMonster;
class CPlayer;
class Barbarian;
class Necromancer;
class CPlayer{ //Base Player Class
private:
int playerHealth; //Begin of self explanatory section
int playerMaxHealth;
int playerMana;
int playerMaxMana;
int playerLevel;
int playerStrength;
int playerSpeed;
int playerDefense;
int playerSkill;
string playerName;
string playerClass; //End of self explanatory section
public:
string skill[10];
CPlayer():
playerHealth(0),
playerMaxHealth(0),
playerMana(0),
playerMaxMana(0),
playerLevel(1),
playerStrength(0),
playerSpeed(0),
playerDefense(0),
playerSkill(1),
playerName(""),
playerClass("")
{
}
int getHealth(){ //Begin Self Explanatory
return playerHealth;
}
int getMaxHealth(){
return playerMaxHealth;
}
int getMana(){
return playerMana;
}
int getMaxMana(){
return playerMaxMana;
}
int getLevel(){
return playerLevel;
}
int getStrength(){
return playerStrength;
}
int getSpeed(){
return playerSpeed;
}
int getDefense(){
return playerDefense;
}
int getSkill(){
return playerSkill;
}
string getName(){
return playerName;
}
string getClass(){
return playerClass;
}
void setHealth(int newhealth){
playerHealth = newhealth;
}
void setMaxHealth(int newmaxhealth){
playerMaxHealth = newmaxhealth;
}
void setMana(int newmana){
playerMana = newmana;
}
void setMaxMana(int newmaxmana){
playerMaxMana = newmaxmana;
}
void setLevel(int newlevel){
playerLevel = newlevel;
}
void setStrength(int newstrength){
playerStrength = newstrength;
}
void setSpeed(int newspeed){
playerSpeed = newspeed;
}
void setDefense(int newdefense){
playerDefense = newdefense;
}
void setSkill(int newskill){
playerSkill = newskill;
}
void setName(string newname){
playerName = newname;
}
void setClass(string newclass){
playerClass = newclass;
} //End of Self Explanatory
void printBaseInfo(){ //Prints the things that are common to all characters
cout<<"Name: "<<playerName<<endl;
cout<<" Level: "<<playerLevel<<endl;
}
void printSkills(){ //Prints all skills known
cout<<"Skills Learned: ";
for(int i = 1; i < 11; i++){
if(playerSkill < i){
break;
}
if(playerSkill > i){
cout<<skill<<"</font>, <font color=darkred>";
}
if(playerSkill == i){
cout<<skill<<endl;
}
}
}
void attack(CMonster *monster);
};
class Barbarian : public CPlayer{ <font color=gray>//Derived barbarian class
</font>
public:
Barbarian(){ <font color=gray>//Default constructor set to what i like for a Barbarian
</font>
setHealth(100);
setMaxHealth(100);
setMana(20);
setMaxMana(20);
setStrength(60);
setSpeed(40);
setDefense(60);
setName(nameSet());
setClass("</font>Barbarian<font color=darkred>");
initSkills();
}
void printInfo(){ <font color=gray>//Prints not always same stuff as other characters.
</font>
system("</font>cls<font color=darkred><font color=darkred>");
cout<<"</font></font>Class: <font color=darkred><font color=darkred>"<<getClass()<<endl;
printBaseInfo();
cout<<"</font></font> Health: <font color=darkred><font color=darkred>"<<getHealth()<<"</font></font>/<font color=darkred><font color=darkred>"<<getMaxHealth()<<endl;
cout<<"</font></font> Mana: <font color=darkred><font color=darkred>"<<getMana()<<"</font></font>/<font color=darkred><font color=darkred>"<<getMaxMana()<<endl;
cout<<"</font></font> Strength: <font color=darkred><font color=darkred>"<<getStrength()<<endl;
cout<<"</font></font> Speed: <font color=darkred><font color=darkred>"<<getSpeed()<<endl;
cout<<"</font></font> Defense: <font color=darkred>"<<getDefense()<<endl;
printSkills();
}
void initSkills(){ <font color=gray>//Initialize barbarians skills
</font>
skill[0] = "</font>Bruise<font color=darkred>";
skill[1] = "</font>Bash<font color=darkred>";
skill[2] = "</font>Crush<font color=darkred>";
skill[3] = "</font>Bang<font color=darkred>";
skill[4] = "</font>Break<font color=darkred>";
skill[5] = "</font>Boom<font color=darkred>";
skill[6] = "</font>Crack<font color=darkred>";
skill[7] = "</font>Shatter<font color=darkred>";
skill[8] = "</font>Destroy<font color=darkred>";
skill[9] = "</font>Annihilate<font color=darkred>";
}
};
class Necromancer : public CPlayer{ <font color=gray>//Derived necromancer class
</font>
public:
Necromancer(){ <font color=gray>//Default constructor stats i want for a necromancer
</font>
setHealth(100);
setMaxHealth(100);
setMana(60);
setMaxMana(60);
setStrength(20);
setSpeed(60);
setDefense(40);
setName(nameSet());
setClass("</font>Necromancer<font color=darkred>");
initSkills();
}
void printInfo(){ <font color=gray>//Same as above
</font>
system("</font>cls<font color=darkred><font color=darkred>");
cout<<"</font></font>Class: <font color=darkred><font color=darkred>"<<getClass()<<endl;
printBaseInfo();
cout<<"</font></font> Health: <font color=darkred><font color=darkred>"<<getHealth()<<"</font></font>/<font color=darkred><font color=darkred>"<<getMaxHealth()<<endl;
cout<<"</font></font> Mana: <font color=darkred><font color=darkred>"<<getMana()<<"</font></font>/<font color=darkred><font color=darkred>"<<getMaxMana()<<endl;
cout<<"</font></font> Strength: <font color=darkred><font color=darkred>"<<getStrength()<<endl;
cout<<"</font></font> Speed: <font color=darkred><font color=darkred>"<<getSpeed()<<endl;
cout<<"</font></font> Defense: <font color=darkred>"<<getDefense()<<endl;
printSkills();
}
void initSkills(){ <font color=gray>//Same as above cept its for necromancer
</font>
skill[0] = "</font>Fire<font color=darkred>";
skill[1] = "</font>Flame<font color=darkred>";
skill[2] = "</font>Burn<font color=darkred>";
skill[3] = "</font>Big Burn<font color=darkred>";
skill[4] = "</font>Blaze<font color=darkred>";
skill[5] = "</font>Firestorm<font color=darkred>";
skill[6] = "</font>Forest Fire<font color=darkred>";
skill[7] = "</font>Flame of Demon<font color=darkred>";
skill[8] = "</font>Smoke Rings<font color=darkred>";
skill[9] = "</font>Hell Fire<font color=darkred>";
}
};
class CMonster{
private:
int health;
int maxhealth;
int defense;
int speed;
int strength;
public:
CMonster(){
health = 100;
maxhealth = 100;
defense = 100;
speed = 100;
strength = 100;
}
int getMonsterHealth(){
return health;
}
void setMonsterHealth(int newhealth){
health = newhealth;
}
int getMonsterDefense(){
return defense;
}
}big;
int chartype;
void getCharacterType(){
cout<<"</font>1) Barbarian\n<font color=darkred><font color=darkred>";
cout<<"</font></font>2) Necromancer\n<font color=darkred><font color=darkred>";
cout<<"</font></font>What kind of character would you like to be?<font color=darkred>";
cin>>chartype;
}
void main(){ <font color=gray>//Main Does nothing haven''t started actual game coding yet
</font>
getCharacterType();
if(chartype == 1){
Barbarian player;
}
else{
Necromancer player;
}
cout<<player.getStrength();
}
string nameSet(){ <font color=gray>//It gets the name from the user when the constructor for
</font>
string charname; <font color=gray>//the class is called;
</font>
cout<<"</font>Enter a name:";
cin>>charname;
<font color=blue>return</font> charname;
}
<font color=blue>void</font> CPlayer::attack(CMonster *monster){
monster->setMonsterHealth((monster->getMonsterHealth() + (.050 * monster->getMonsterDefense())) - (getStrength()));
}
</pre><!–ENDSCRIPT–>
The error is player not defined… I hate asking so many questions but I couldn''t find out this information anywhere else.
Thanks yet again.
<hr>
<font face="" color="Blue">Favorite Quotes:</font><font face="" color="red">Gandalf: You shall not pass!</font>|<font face="" color="orange">Smeagol: We don''t need you!</font>|<font face="" color="violet">Sloth: Hey you guys!</font>|
<hr>
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|