// =========================================================================== // MyVector, MyVector2D, and NNMyVector2D // =========================================================================== #include #include using namespace std; // The definition of MyVector class MyVector { friend const MyVector operator+(const MyVector& v, double d); protected: int n; double* m; public: MyVector(); MyVector(int n, double m[]); MyVector(const MyVector& v); ~MyVector(); void print() const; bool operator==(const MyVector& v) const; bool operator!=(const MyVector& v) const; bool operator<(const MyVector& v) const; double operator[](int i) const; double& operator[](int i); const MyVector& operator=(const MyVector& v); const MyVector& operator+=(const MyVector& v); }; MyVector::MyVector(): n(0), m(nullptr) { } MyVector::MyVector(int n, double m[]) { this->n = n; this->m = new double[n]; for(int i = 0; i < n; i++) this->m[i] = m[i]; } MyVector::MyVector(const MyVector& v) { this->n = v.n; this->m = new double[n]; for(int i = 0; i < n; i++) this->m[i] = v.m[i]; } MyVector::~MyVector() { delete [] m; } void MyVector::print() const { cout << "("; for(int i = 0; i < n - 1; i++) cout << m[i] << ", "; cout << m[n-1] << ")\n"; } bool MyVector::operator==(const MyVector& v) const { if(this->n != v.n) return false; else { for(int i = 0; i < n; i++) { if(this->m[i] != v.m[i]) return false; } } return true; } bool MyVector::operator!=(const MyVector& v) const { return !(*this == v); } bool MyVector::operator<(const MyVector& v) const { if(this->n != v.n) return false; else { for(int i = 0; i < n; i++) { if(this->m[i] >= v.m[i]) return false; } } return true; } double MyVector::operator[](int i) const { if(i < 0 || i >= n) exit(1); return m[i]; } double& MyVector::operator[](int i) { if(i < 0 || i >= n) exit(1); return m[i]; } const MyVector& MyVector::operator=(const MyVector& v) { if(this != &v) { if(this->n != v.n) { delete [] this->m; this->n = v.n; this->m = new double[this->n]; } for(int i = 0; i < n; i++) this->m[i] = v.m[i]; } return *this; } const MyVector& MyVector::operator+=(const MyVector& v) { if(this->n == v.n) { for(int i = 0; i < n; i++) this->m[i] += v.m[i]; } return *this; } // end of the definition of MyVector // The definition of MyVector2D class MyVector2D : public MyVector { public: MyVector2D(); MyVector2D(double m[]); void setValue(double i1, double i2); }; MyVector2D::MyVector2D() { this->n = 2; } MyVector2D::MyVector2D(double m[]) : MyVector(2, m) { } void MyVector2D::setValue(double i1, double i2) { if(this->m == nullptr; this->m = new double[2]; this->m[0] = i1; this->m[1] = i2; } // end of the definition of MyVector2D // The definition of NNVector2D class NNVector2D : public MyVector2D { public: NNVector2D(); NNVector2D(double m[]); void setValue(double i1, double i2); }; NNVector2D::NNVector2D() { } NNVector2D::NNVector2D(double m[]) { this->m = new double[2]; this->m[0] = m[0] >= 0 ? m[0] : 0; this->m[1] = m[1] >= 0 ? m[1] : 0; } void NNVector2D::setValue(double i1, double i2) { if(this->m == nullptr) this->m = new double[2]; this->m[0] = i1 >= 0 ? i1 : 0; this->m[1] = i2 >= 0 ? i2 : 0; } // end of the definition of NNVector2D // Global functions headers const MyVector operator+(const MyVector& v, double d); const MyVector operator+(double d, const MyVector& v); const MyVector operator+(const MyVector& v1, const MyVector& v2); // end of global function headers // Global function definitions const MyVector operator+(const MyVector& v, double d) { MyVector sum(v); for(int i = 0; i < v.n; i++) sum[i] += d; return sum; } const MyVector operator+(double d, const MyVector& v) { return v + d; } const MyVector operator+(const MyVector& v1, const MyVector& v2) { MyVector sum(v1); return sum += v2; } // end of global function definitions // =========================================================================== // End of MyVector, MyVector2D, and NNMyVector2D // =========================================================================== // =========================================================================== // Character, Warrior, Wizard, and Team // =========================================================================== #include #include #include using namespace std; class Character { protected: static const int EXP_LV = 100; string name; int level; int exp; int power; int knowledge; int luck; void levelUp(int pInc, int kInc, int lInc); public: Character(string n, int lv, int po, int kn, int lu); virtual void beatMonster(int exp) = 0; virtual void print(); string getName(); }; Character::Character(string 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) { } void Character::print() { cout << this->name << ": Level " << this->level << " (" << this->exp << "/" << pow(this->level, 2) * EXP_LV << "), " << this->power << "-" << this->knowledge << "-" << this->luck << "\n"; } void Character::levelUp(int pInc, int kInc, int lInc) { this->level++; this->power += pInc; this->knowledge += kInc; this->luck += lInc; } string Character::getName() { return this->name; } 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(string n) : Character(n, 1, PO_LV, KN_LV, LU_LV) {} Warrior(string n, int lv) : Character(n, lv, lv * PO_LV, lv * KN_LV, lv * LU_LV) {} void print() { cout << "Warrior "; Character::print(); } void beatMonster(int exp) { this->exp += exp; while(this->exp >= pow(this->level, 2) * EXP_LV) this->levelUp(PO_LV, KN_LV, LU_LV); } }; 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(string n) : Character(n, 1, PO_LV, KN_LV, LU_LV) {} Wizard(string n, int lv) : Character(n, lv, lv * PO_LV, lv * KN_LV, lv * LU_LV) {} void print() { cout << "Wizard "; Character::print(); } void beatMonster(int exp) { this->exp += exp; while(this->exp >= pow(this->level, 2) * EXP_LV) this->levelUp(PO_LV, KN_LV, LU_LV); } }; //===================================================== // oop(p. 49) class Team { private: int memberCount; Character* member[10]; public: Team(); ~Team(); void addWarrior(string name, int lv); void addWizard(string name, int lv); void memberBeatMonster(string name, int exp); void printMember(string name); }; //====================================================== //opp(p. 50) Team::Team() { this->memberCount = 0; for(int i = 0; i < 10; i++) member[i] = nullptr; } Team::~Team() { for(int i = 0; i < this->memberCount; i++) delete this->member[i]; } void Team::addWarrior(string name, int lv) { if(memberCount < 10) { member[memberCount] = new Warrior(name, lv); memberCount++; } } void Team::addWizard(string name, int lv) { if(memberCount < 10) { member[memberCount] = new Wizard(name, lv); memberCount++; } } //====================================================== //opp(p. 51) void Team::memberBeatMonster (string name, int exp) { for(int i = 0; i < this->memberCount; i++) { if(this->member[i]->getName() == name) { this->member[i]->beatMonster(exp); break; } } } void Team::printMember(string name) { for(int i = 0; i < this->memberCount; i++) { if(this->member[i]->getName() == name) { this->member[i]->print(); break; } } } // =========================================================================== // End of Character, Warrior, Wizard, and Team // ===========================================================================