|
| 1 | +#include <iostream> |
| 2 | +#include <string> |
| 3 | +#include <sstream> |
| 4 | +using namespace std; |
| 5 | + |
| 6 | +class Soldier |
| 7 | +{ |
| 8 | +private: |
| 9 | + int _hitPoint; |
| 10 | + int _mana; |
| 11 | + |
| 12 | +public: // Property |
| 13 | + int getHitPoint() { return _hitPoint; } |
| 14 | + int getMana() { return _mana; } |
| 15 | + |
| 16 | + void setHitPoint(int hitPoint) { _hitPoint = hitPoint; } |
| 17 | + void setMana(int mana) { _mana = mana; } |
| 18 | + |
| 19 | +public: // Constructor |
| 20 | + Soldier() { _hitPoint = 0; _mana = 0; } |
| 21 | + Soldier(int hitPoint, int mana) { _hitPoint = hitPoint; _mana = mana; } |
| 22 | + |
| 23 | +public: // Behaviors |
| 24 | + void attack() |
| 25 | + { |
| 26 | + cout << "The Soldier is attacking." << endl; |
| 27 | + } |
| 28 | + void defend() |
| 29 | + { |
| 30 | + cout << "The Soldier is defending." << endl; |
| 31 | + } |
| 32 | + |
| 33 | +public: |
| 34 | + // Print |
| 35 | + string toString() |
| 36 | + { |
| 37 | + stringstream ss; |
| 38 | + ss << "HP: " << _hitPoint << " Mana: " << _mana << endl; |
| 39 | + return ss.str(); |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +class Fraction |
| 44 | +{ |
| 45 | +private: |
| 46 | + int _numerator; |
| 47 | + int _denominator; |
| 48 | + |
| 49 | +public: |
| 50 | + int getNumerator() { return _numerator; } |
| 51 | + int getDenominator() { return _denominator; } |
| 52 | + |
| 53 | + void setNumerator(int numerator) { _numerator = numerator; } |
| 54 | + void setDenominator(int denominator) { _denominator = denominator; } |
| 55 | + |
| 56 | +public: |
| 57 | + Fraction() { _numerator = 0; _denominator = 0; } |
| 58 | + |
| 59 | + Fraction(int numerator, int denominator) { _numerator = numerator; _denominator = denominator; } |
| 60 | + |
| 61 | +public: |
| 62 | + string toString() |
| 63 | + { |
| 64 | + stringstream ss; |
| 65 | + ss << "Numerator: " << _numerator << " Denominator: " << _denominator << endl; |
| 66 | + ss << "The fraction is: " << _numerator << "/" << _denominator << endl; |
| 67 | + return ss.str(); |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +class Point |
| 72 | +{ |
| 73 | +private: |
| 74 | + int _x; |
| 75 | + int _y; |
| 76 | + |
| 77 | +public: |
| 78 | + int getX() { return _x; } |
| 79 | + int getY() { return _y; } |
| 80 | + |
| 81 | + void setX(int x) { _x = x; } |
| 82 | + void setY(int y) { _y = y; } |
| 83 | + |
| 84 | +public: |
| 85 | + Point() { _x = 0; _y = 0; } |
| 86 | + Point(int x, int y) { _x = x; _y = y; } |
| 87 | + |
| 88 | +public: |
| 89 | + string toString() { |
| 90 | + stringstream ss; |
| 91 | + ss << "X: " << _x << " Y: " << _y << endl; |
| 92 | + ss << "The point is: " << "(" << _x << "," << _y << ")" << endl; |
| 93 | + return ss.str(); |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +class Line |
| 98 | +{ |
| 99 | +private: |
| 100 | + Point _a; |
| 101 | + Point _b; |
| 102 | + |
| 103 | +public: |
| 104 | + int getFirstX() { return _a.getX(); } |
| 105 | + int getFirstY() { return _a.getY(); } |
| 106 | + int getLastX() { return _b.getX(); } |
| 107 | + int getLastY() { return _b.getY(); } |
| 108 | + |
| 109 | + void setFirst(int x, int y) { _a.setX(x); _a.setY(y); } |
| 110 | + void setLast(int x, int y) { _b.setX(x); _b.setY(y); } |
| 111 | +public: |
| 112 | + Line() |
| 113 | + { |
| 114 | + _a.setX(0); |
| 115 | + _a.setY(0); |
| 116 | + _b.setX(0); |
| 117 | + _b.setY(0); |
| 118 | + } |
| 119 | + Line(int x1, int y1, int x2, int y2) { |
| 120 | + _a.setX(x1); |
| 121 | + _a.setY(y1); |
| 122 | + _b.setX(x2); |
| 123 | + _b.setY(y2); |
| 124 | + } |
| 125 | + |
| 126 | +public: |
| 127 | + string toString() |
| 128 | + { |
| 129 | + stringstream ss; |
| 130 | + ss << "The line contains: " << endl << "First point: (" << _a.getX() << ", " << _a.getY() << ")" << endl |
| 131 | + << "Last point: (" << _b.getX() << ", " << _b.getY() << ")" << endl; |
| 132 | + return ss.str(); |
| 133 | + } |
| 134 | +}; |
| 135 | + |
| 136 | +int main() |
| 137 | +{ |
| 138 | + Soldier s(100, 100); |
| 139 | + cout << s.toString(); |
| 140 | + s.attack(); |
| 141 | + s.defend(); |
| 142 | + cout << "---------------------------------------------" << endl; |
| 143 | + Fraction f(100, 100); |
| 144 | + cout << f.toString(); |
| 145 | + cout << "---------------------------------------------" << endl; |
| 146 | + Point p(100, 100); |
| 147 | + cout << p.toString(); |
| 148 | + cout << "---------------------------------------------" << endl; |
| 149 | + Line l(0, 0, 1, 1); |
| 150 | + cout << l.toString(); |
| 151 | + return 0; |
| 152 | +} |
0 commit comments