Here's the code:
stats.h
Code:
class Stats
{
public:
Stats();
~Stats();
void power (int pow);
void vitality (int vit);
void magic (int mag);
void maxhealth (int maxh);
void health (int hea);
void maxmana (int maxm);
void mana (int man);
int displaypower();
int displayvitality();
int displaymagic();
int displaymaxhealth();
int displayhealth();
int displaymaxmana();
int displaymana();
protected:
unsigned int strength;
unsigned int defense;
unsigned int intelligence;
unsigned int maxhp;
signed int hp;
unsigned int maxmp;
unsigned int mp;
};
class Hero :public Stats
{
public:
Hero();
~Hero();
void level_up();
void level_stats();
void char_class_stats();
//1-human 2-dwarf 3-elf
//character
Hero character;
character.power(pow);
character.vitality(vit);
character.magic(mag);
character.maxhealth(maxh);
character.health(hea);
character.maxmana(maxm);
character.mana(man);
//human
Hero human;
human.power(10);
human.vitality(10);
human.magic(10);
human.maxhealth(100);
human.health(100);
human.maxmana(10);
human.mana(10);
//dwarf
Hero dwarf;
dwarf.power(15);
dwarf.vitality(15);
dwarf.magic(5);
dwarf.maxhealth(150);
dwarf.health(150);
dwarf.maxmana(5);
dwarf.mana(5);
//elf
Hero elf;
elf.power(5);
elf.vitality(10);
elf.magic(15);
elf.maxhealth(50);
elf.health(50);
elf.maxmana(15);
elf.mana(15);
protected:
unsigned int char_level;
unsigned int char_exp;
unsigned int char_class;
};
//stat function declarations start
Stats::Stats()
{}
Stats::~Stats()
{}
Hero::Hero()
{}
Hero::~Hero()
{}
void Stats::power (int pow)
{
strength=pow;
}
void Stats::vitality (int vit)
{
defense=vit;
}
void Stats::magic (int mag)
{
intelligence=mag;
}
void Stats::maxhealth (int maxh)
{
maxhp=maxh;
}
void Stats::health (int hea)
{
hp=hea;
}
void Stats::maxmana (int maxm)
{
maxmp=maxm;
}
void Stats::mana (int man)
{
mp=man;
}
int Stats::displaypower()
{
return strength;
}
int Stats::displayvitality()
{
return defense;
}
int Stats::displaymagic()
{
return intelligence;
}
int Stats::displaymaxhealth()
{
return maxhp;
}
int Stats::displayhealth()
{
return hp;
}
int Stats::displaymaxmana()
{
return maxmp;
}
int Stats::displaymana()
{
return mp;
}
//stat function declarations end
//character class start
void Hero::char_class_stats()
{
switch (char_class)
{
case 1:
character.power(pow) = human.power(pow);
character.vitality(vit) = human.vitality(vit);
character.magic(mag) = human.magic(mag);
character.maxhealth(maxh) = human.maxhealth(maxh);
character.health(hea) = human.health(hea);
character.maxmana(maxm) = human.maxmana(maxm);
character.mana(man) = human.mana(man);
break;
case 2:
character.power(pow) = dwarf.power(pow);
character.vitality(vit) = dwarf.vitality(vit);
character.magic(mag) = dwarf.magic(mag);
character.maxhealth(maxh) = dwarf.maxhealth(maxh);
character.health(hea) = dwarf.health(hea);
character.maxmana(maxm) = dwarf.maxmana(maxm);
character.mana(man) = dwarf.mana(man);
break;
case 3:
character.power(pow) = elf.power(pow);
character.vitality(vit) = elf.vitality(vit);
character.magic(mag) = elf.magic(mag);
character.maxhealth(maxh) = elf.maxhealth(maxh);
character.health(hea) = elf.health(hea);
character.maxmana(maxm) = elf.maxmana(maxm);
character.mana(man) = elf.mana(man);
break;
}
}
//character class end
//levels start
void Hero::level_stats()
{
unsigned int char_level=1;
switch (char_level)
{
case 1:
character.power(pow) = character.power(pow);
character.vitality(vit) = character.vitality(vit);
character.magic(mag) = character.magic(mag);
character.maxhealth(maxh) = character.maxhealth(maxh);
character.health(hea) = character.health(hea);
character.maxmana(maxm) = character.maxmana(maxm);
character.mana(man) = character.mana(man);
break;
case 2:
character.power(pow) = character.power(pow) + 2;
character.vitality(vit) = character.vitality(vit) + 2;
character.magic(mag) = character.magic(mag) + 2;
character.maxhealth(maxh) = character.maxhealth(maxh) + 20;
character.health(hea) = character.health(hea) + 20;
character.maxmana(maxm) = character.maxmana(maxm)+ 5;
character.mana(man) = character.mana(man) + 5;
break;
case 3:
character.power(pow) = character.power(pow) + 4;
character.vitality(vit) = character.vitality(vit) + 4;
character.magic(mag) = character.magic(mag) + 4;
character.maxhealth(maxh) = character.maxhealth(maxh) + 40;
character.health(hea) = character.health(hea) + 40;
character.maxmana(maxm) = character.maxmana(maxm)+ 10;
character.mana(man) = character.mana(man) + 10;
break;
}
}
void Hero::level_up()
{
if ((char_exp > 50) && (char_level==1))
{
char_level++;
cout<<"\nYou are now level"<<char_level<<"!\n\n";
}
else if ((char_exp > 100) && (char_level==2))
{
char_level++;
cout<<"\nYou are now level"<<char_level<<"!\n\n";
}
}
//levels end
main.cpp
Code:
#include <string>
#include <iostream>
#include "stats.h"
using namespace std;
int main()
{
cin.get();
}
It won't compile for the same reason about 50 times: ISO C++ forbids declaration of 'character' with no type.
What I tried to do in the inherited class Hero was declare an instance of Hero as each race (or char_class). Why won't it work? Should I change it to, let's say, Stats character, character.power(int pow), etc...? What could I do to make this work? Help is extremely appreciated.