// =========================================================================== // Using templates to revise Character, Warrior, Wizard, and Team // =========================================================================== #include #include #include using namespace std; template class Character { protected: static const int EXP_LV = 100; KeyType name; int level; int exp; int power; int knowledge; int luck; void levelUp(int pInc, int kInc, int lInc); public: Character(KeyType n, int lv, int po, int kn, int lu); virtual void beatMonster(int exp) = 0; virtual void print(); KeyType getName(); }; template Character::Character(KeyType n, int lv, int po, int kn, int lu) : name(n), level(lv), exp(pow(lv - 1, 2) * EXP_LV), power(po), knowledge(kn), luck(lu) { } template void Character::print() { cout << this->name << ": Level " << this->level << " (" << this->exp << "/" << pow(this->level, 2) * EXP_LV << "), " << this->power << "-" << this->knowledge << "-" << this->luck << "\n"; } template void Character::levelUp(int pInc, int kInc, int lInc) { this->level++; this->power += pInc; this->knowledge += kInc; this->luck += lInc; } template KeyType Character::getName() { return this->name; } template class Warrior : public Character { private: static const int PO_LV = 10; static const int KN_LV = 5; static const int LU_LV = 5; public: Warrior(KeyType n, int lv = 0); void print(); void beatMonster(int exp); }; template Warrior::Warrior(KeyType n, int lv) : Character(n, lv, lv * PO_LV, lv * KN_LV, lv * LU_LV) { } template void Warrior::print() { cout << "Warrior "; Character::print(); } template void Warrior::beatMonster(int exp) { this->exp += exp; while(this->exp >= pow(this->level, 2) * Character::EXP_LV) this->levelUp(PO_LV, KN_LV, LU_LV); } template class Wizard : public Character { private: static const int PO_LV = 4; static const int KN_LV = 9; static const int LU_LV = 7; public: Wizard(KeyType n, int lv = 0); void print(); void beatMonster(int exp); }; template Wizard::Wizard(KeyType n, int lv) : Character(n, lv, lv * PO_LV, lv * KN_LV, lv * LU_LV) { } template void Wizard::print() { cout << "Wizard "; Character::print(); } template void Wizard::beatMonster(int exp) { this->exp += exp; while(this->exp >= pow(this->level, 2) * Character::EXP_LV) this->levelUp(PO_LV, KN_LV, LU_LV); } template class Team { private: int memberCount; Character* member[10]; public: Team(); ~Team(); void addWarrior(KeyType name, int lv); void addWizard(KeyType name, int lv); void memberBeatMonster(KeyType name, int exp); void printMember(KeyType name); }; template Team::Team() { this->memberCount = 0; for(int i = 0; i < 10; i++) member[i] = nullptr; } template Team::~Team() { for(int i = 0; i < this->memberCount; i++) delete this->member[i]; } template void Team::addWarrior(KeyType name, int lv) { if(memberCount < 10) { member[memberCount] = new Warrior(name, lv); memberCount++; } } template void Team::addWizard(KeyType name, int lv) { if(memberCount < 10) { member[memberCount] = new Wizard(name, lv); memberCount++; } } template void Team::memberBeatMonster(KeyType name, int exp) { for(int i = 0; i < this->memberCount; i++) { if(this->member[i]->getName() == name) { this->member[i]->beatMonster(exp); break; } } } template void Team::printMember(KeyType name) { for(int i = 0; i < this->memberCount; i++) { if(this->member[i]->getName() == name) { this->member[i]->print(); break; } } } // =========================================================================== // End of using templates to revise Character, Warrior, Wizard, and Team // =========================================================================== // =========================================================================== // Using vectors to revise Team // =========================================================================== #include #include #include using namespace std; template class Team { private: vector*> member; public: Team(); ~Team(); void addWarrior(KeyType name, int lv); void addWizard(KeyType name, int lv); void memberBeatMonster(KeyType name, int exp); void printMember(KeyType name); }; template Team::Team() { } template Team::~Team() { while(this->member.size() > 0) { delete this->member.back(); this->member.pop_back(); } } template void Team::addWarrior(KeyType name, int lv) { Warrior* wPtr = new Warrior(name, lv); this->member.push_back(wPtr); } template void Team::addWizard(KeyType name, int lv) { Wizard* wPtr = new Wizard(name, lv); this->member.push_back(wPtr); } template void Team::memberBeatMonster(KeyType name, int exp) { for(int i = 0; i < this->member.size(); i++) { if(this->member[i]->getName() == name) { this->member[i]->beatMonster(exp); break; } } } template void Team::printMember(KeyType name) { for(int i = 0; i < this->member.size(); i++) { if(this->member[i]->getName() == name) { this->member[i]->print(); break; } } } // =========================================================================== // End of using vector to revise Team // =========================================================================== int main() { Team t; t.addWarrior("Alice", 1); t.memberBeatMonster("Alice", 10000); t.addWizard("Bob", 2); t.printMember("Alice"); Team t2; t2.addWarrior(1, 1); t2.memberBeatMonster(1, 10000); t2.addWizard(2, 2); t2.printMember(1); return 0; }