I’m finally covering Object Oriented Programming with C++! I cover classes, objects, public, private, protected, constructors, deconstructors, pointer operator, dot operator, getters, setters, super classes, inheritance, calling super class functions, overwriting functions, and much more.
In the problem with this tutorial we will try to model a battle between the Hulk and Thor to the death. All of the code follows down below.
If you enjoy videos like this, consider donating $1, or simply turn off Ad Blocking Software. Either helps me to continue making free videos.
Code & Transcript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
// ---------- INTRO TO CLASSES & INHERITANCE ---------- #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <sys/types.h> #include <dirent.h> #include <sys/stat.h> class Animal{ // Private variables are only available to methods // in the class private: std::string name; double height; double weight; // Static variables share the same value for all // objects of the Animal class static int numOfAnimals; // Public fields and methods can be accessed by // anything with access to the object public: std::string GetName(){return name;} // Because we don't know what an objects name is // because the class is created before the object // we can refer to all objects using this // The pointer operator is used to access an // objects fields and methods void SetName(std::string name){this->name = name;} double GetHeight(){return height;} void SetHeight(double height){this->height = height;} double GetWeight(){return weight;} void SetWeight(double weight){this->weight = weight;} // You can declare function prototypes void SetAll(std::string, double, double); // A constructor is called each time an object is created Animal(std::string, double, double); // Create an overloaded constructor for when no data is passed Animal(); // A deconstructor is called automatically when an object // is deleted or is no longer used // The default is fine, but you should create custom ones // when you must release memory, or resources ~Animal(); // Static methods can only access static fields static int GetNumOfAnimals(){return numOfAnimals;} // Created to be overwritten void ToString(); }; // Refer to class fields and methods with :: int Animal::numOfAnimals = 0; // Define the prototype method void Animal::SetAll(std::string name, double height, double weight){ this->name = name; this->weight = weight; this->height = height; Animal::numOfAnimals++; } // Define the constructor Animal::Animal(std::string name, double height, double weight){ this->name = name; this->weight = weight; this->height = height; Animal::numOfAnimals++; } Animal::Animal(){ this->name = ""; this->weight = 0; this->height = 0; Animal::numOfAnimals++; } Animal::~Animal(){ std::cout << "Animal " << this -> name << " destroyed\n"; } void Animal::ToString(){ std::cout << this -> name << " is " << this -> height << " cms tall and " << this -> weight << " kgs in weight\n"; } // Through inheritance a class inherits all fields and methods // defined by the super, or inherited from class class Dog: public Animal{ private: std::string sound = "Wooof"; public: // You can access to the private field name // by calling GetName() void MakeSound(){ std::cout << "The dog " << this->GetName() << " says " << this->sound << "\n"; } // The Dogs constructor Dog(std::string, double, double, std::string); // The default constructor calls Animals default // constructor Dog(): Animal(){}; // Overwrite ToString void ToString(); }; // Calls the superclasses constructor to handle // initalization Dog::Dog(std::string name, double height, double weight, std::string sound) : Animal(name, height, weight){ this -> sound = sound; } // Overwrite ToString void Dog::ToString(){ // Because the attributes were private in Animal they must be retrieved // by called the get methods std::cout << this -> GetName() << " is " << this -> GetHeight() << " cms tall, " << this -> GetWeight() << " kgs in weight and says " << this -> sound << "\n"; } int main() { // Create object without setting values in constructor Animal fred; fred.SetHeight(33); fred.SetWeight(10); fred.SetName("Fred"); // Get the values for the Animal fred.ToString(); fred.SetAll("Fred", 34, 12); fred.ToString(); // Setting values with constructor Animal tom("Tom", 36, 15); tom.ToString(); // Demonstrate inherited Dog class Dog spot("Spot", 38, 16, "Wooof"); // See different output from overwritten ToString() spot.ToString(); // Call static methods by using the class name to // show the total Animals created std::cout << "Number of Animals " << Animal::GetNumOfAnimals() << "\n"; return 0; } // ---------- END INTRO TO CLASSES & INHERITANCE ---------- // ---------- PROBLEM WARRIORS BATTLE TILL DEATH ---------- // Make this /* Thor attacks Hulk and deals 12 damage Hulk is down to 28 health Hulk attacks Thor and deals 3 damage Thor is down to 37 health Thor attacks Hulk and deals 14 damage Hulk is down to 14 health Hulk attacks Thor and deals 0 damage Thor is down to 37 health Thor attacks Hulk and deals 14 damage Hulk is down to 0 health Hulk has Died and Thor is Victorious Game Over */ #include <cstdlib> #include <iostream> #include <string> #include <vector> #include <ctime> #include <numeric> #include <math.h> class Warrior{ private: int attkMax; int blockMax; public: std::string name; int health; Warrior(std::string name, int health, int attkMax, int blockMax){ this->name = name; this->health = health; this->attkMax = attkMax; this->blockMax = blockMax; } // The attack and block amount will be random int Attack(){ return std::rand() % this->attkMax; } int Block(){ return std::rand() % this->blockMax; } }; class Battle{ public: // We pass warriors into the function by reference so we can // track continued damage to each // We continue to loop having each warrior take turns attacking // until a warriors health < 0 static void StartFight(Warrior& warrior1, Warrior& warrior2){ while(true){ if(Battle::GetAttackResult(warrior1, warrior2).compare("Game Over") == 0){ std::cout << "Game Over\n"; break; } if(Battle::GetAttackResult(warrior2, warrior1).compare("Game Over") == 0){ std::cout << "Game Over\n"; break; } } } static std::string GetAttackResult(Warrior& warriorA, Warrior& warriorB){ // Get random attack & block amounts and calculate damage int warriorAAttkAmt = warriorA.Attack(); int warriorBBlockAmt = warriorB.Block(); int damage2WarriorB = ceil(warriorAAttkAmt - warriorBBlockAmt); // Change health total if > 0 and output changes damage2WarriorB = (damage2WarriorB <= 0) ? 0 : damage2WarriorB; warriorB.health = warriorB.health - damage2WarriorB; printf("%s attacks %s and deals %d damage\n", warriorA.name.c_str(), warriorB.name.c_str(), damage2WarriorB); printf("%s is down to %d health\n", warriorB.name.c_str(), warriorB.health); // Once health < 0 end game by passing back Game Over if(warriorB.health <= 0){ printf("%s has Died and %s is Victorious\n", warriorB.name.c_str(), warriorA.name.c_str()); return "Game Over"; } else { return "Fight Again"; } } }; int main() { // Seed the random number generator srand(time(NULL)); // Create warriors Warrior tom("Thor", 100, 35, 15); Warrior sam("Hulk", 125, 25, 10); // Create battle and have it run on its own till completion Battle::StartFight(tom, sam); return 0; } // ---------- END PROBLEM WARRIORS BATTLE TILL DEATH ---------- |
Leave a Reply