Symbols && operator, 32 operator, 32 A AddDynamicOption method, 188 AddItem method, 192 Addition operator, 18 Allocating memory, 228 overloaded delete function, 233 overloaded new function, 232 unnamed namespace, 232 AND operator, 28 AndroidEnemy class, 282 Apple LLVM, 2 Arithmetic calculator, 20 Arithmetic operations addition, 18 division operator, 19 modulo, 19 multiplication, 19 subtraction, 18 Arithmetic unary operators, 33 assert method, 204 Assignment operators, 17 invoking, 142 Player class, 141 at method, 175 AttackEnemyOption class, 193 B basic_string capacity methods, 149 Binary (OR) operator, 30 Bitwise operators AND operator, 28 Binary (OR) operator, 30 bit sequence, 27 ^ (exclusive OR) operator, 30 hexadecimal number, 27 left shift (<<) operator, 31 right shift (>>) operator, 32 C C++ const keyword constant member methods, 105 constant variables, 104 parameters, 105 pointers, 105 friend keyword, 108 inline keyword, 107 static keyword global and file scoped variables, 102 static class member variables, 98 static local variables, 98 static member methods, 101 C++98, 1 Calculators equality operators, 23 greater-than operator, 25 greater-than or equal to operator, 25 inequality operator, 24 less than operator, 26 less-than or equal to operator, 26 C++ array, 37 C++ guessing game boolean values, 11 dynamics vs. static typed languages declaring variables, 7 defining variables, 8 enums, 11 12 floating point numbers, 10 integers, 8 9 291
292 Index C++ guessing game (cont.) rand function, 15 srand function, 15 static_cast, 13 two s complement, 9 10 unsigned integers, 9 CHAR_BIT constant, 278 char data type, 27 Chest class, 189 Clang, 2 Class inheritance IDE, 113 logging code, 115 method overriding, 117 motorcycle class, 114 printing number of wheels, 113 protected keyword, 113 public inheritance, 113 Text Adventure Entity class, 117 GivePlayerOptions and EvaluateInput methods, 123 InitializeRooms, 120 Player class, 118 Room array, 120 Room class, 118 Room pointer, 121 122 RunGame, 121, 123 UpdateOnOption method, 124 vehicle class hierarchy, 111 vehicle constructor, 114 vehicle destructor, 116 C++ multiple platforms AndroidEnemy class, 282 bad pointer cast, 279 CHAR_BIT constant, 278 CompareAddresses function, 279 CreateEnemy function, 283 EnemyFactory updation, 282 FixedTypes.h, 278 good pointer comparison, 279 iosenemy class, 282 Platforms.h, 280 renaming Enemy to EnemyBase, 280 types, 277 WindowsEnemy class, 282 Compilers, 2, 201 constexpr keyword, 203 Contiguous memory, 163 Copy constructors constructor method, 140 value and constant reference, 140 Coupled code, 244 C++ programming language compilers, 2 CPU game.rungame() method, 268 RunGameThread function, 268 thread creation, 268 data sharing threads Game::HasFinished method, 269 mutex (see Mutex) _tmain crash function, 269 game developers, 3 LoadSaveGame function future::wait_for method, 274 get_future method, 274 packaged_task, 273 Microsoft Visual Studio Express Windows Desktop 2012, 3 paradigms (see Programming paradigms) text adventure Game class definition, 285 Game::RunGame method, 287 paired_task, 288 std::thread::get_future method, 288 C style strings null terminator, 43 strcat, 45 strcmp, 44 strcpy, 45 string literal, 43 strlen, 44 D Dereferencing pointers, 41 Design patterns factory advantage, 241 Enemy class, 241 243 Game class, 241
293 Option class, 242 Option creation, 240 pointer aliases, 243 shared_ptr, 243 updated Game constructor, 243 observers AddObserver and RemoveObserver method, 245 decoupling code, 244 Game class constructor and destructor, 248 Game class QuitObserver, 247 Notifier template class, 244 Notify method, 245 OnQuit method, 248 QuitObserver class, 246 QuitOption, 246 singleton, 239 Visitor class, 249 EvaluateVisitor class, 250 Game::EvaluateInput method, 251 Option class, 250 Text Adventure class, 249 Visitable class, 249 Division operator, 19 E emplace method, 169 Enemy class, 192 Equality operators, 22 EvaluateInput method, 136 F Files and namespaces creation, 73 GameLoop.cpp file, 75, 77 GameLoop.h, 75 HelloWorld.cpp, 71 HelloWorld from _tmain, 72 HelloWorld.h file, 72 Player.h, 75 PlayerOptions.h, 77 #pragma once, 72 TextAdventure.cpp file, 74 find_first_of method, 153 Find method, 152, 173 Flow control continue and break statement, 64 else and else if statements, 58 60 for loop, 60 61 goto statement, 65 66 if statement, 57 switch statement, 62 64 Text Adventure, game loop chosenoption, 67 compare function, 67 GetPlayerInput function, 67 GivePlayerOptions, 67 main function, 68 PlayerOptions, 67 Player struct and WelcomePlayer function, 66 RunGame function, 68 while loop, 61 62 Functions parameters, 50 pointer, 51 PrintHello function, 50 reference, 52 return values, 51 single function, 49 structures, 53 54 Text Adventure game, 54 55 G Game class, 287 Game::OnQuit method, 271 Game RunGame method, 259 Generic programming, 3 GetCounter and IncrementCounter functions, 107 Global namespace, 73 GNU Compiler Collection (GCC), 2 H HasFinished method, 269 270 HasWeapon method, 192 I, J, K Increment and decrement operators, 33 IncrementCounter method, 101 Inequality operator, 24
294 Index insert method, 169, 171 intarray, 42 Integrated development environment (IDE), 113 iosenemy class, 282 Item class, 190 L Left shift (<<) operator, 31 ListNode class, 164 for loop function, 166 OurList function, 165 ListNode second, 166 list::sort method, 167 Lock method, 270 Logical operators && operator, 32 operator, 32 M Memory management constructed text, 236 C++ stack memory function1, 226 program, 226 stack frame, 227 _tmain, 226 heap memory allocating memory, 228 new and delete operators, 228 229 plast and pcurrent, 233 STL class, 229 unique_ptr and shared_ptr, 230 MemoryAllocationHeader struct, 232 memory heap, 234 overloaded delete function, 233 overloaded new function, 232 PrintAllocations function, 234 pstart and pnextfree, 233 static memory, 225 unnamed namespace, 232 Microsoft Visual C++ Compiler (MSVC), 2 Microsoft Visual Studio Express 2012, 3 Modulo operator, 19 Multiplication operator, 19 Mutex, 287 HasFinished method, 269 270 lock method, 270 OnQuit method, 271 RunGame method, 271 272 SetPlayerQuit and SetPlayerWon methods, 271 _tmain function, 269 try_lock method, 270 unlock method, 270 N Native Development Kit (NDK)., 2 Non-inline method, 108 Notify method, 245 Null terminator, 43 O Object-oriented programming (OOP), 2 default constructor, 86 encapsulation class vs. struct, 83 Player class, 83 84 public class, 84 operator overloading, 90 overload methods, 88 Player Class constructor, 85 Player constructor, 87 string class constructor, 85 Text Adventure game Game class, 92 Game.cpp, 93 GameLoop namespace, 92 main function, 95 Player class, 94 OpenChestOptions class, 191 Open method, 190 Operators arithmetic (see Arithmetic operations) assignment, 17 bitwise (see Bitwise operators) logical (see Logical operators) relational (see Relational operators) unary (see Unary operators) P, Q Player copy constructor, 140 Player::HasWeapon method, 194 Player OnLoad method, 262
295 Player OnSave method, 261 262 Pointers arithmetic, 41 array, 42 dereference operator, 41 low-level programming language, 39 memory address., 39 output, 40 Polymorphism definition, 127 dynamic_cast downcasting, 129 upcasting, 130 pure virtual methods, 130 Text Adventure EvaluateInput method, 136 Game Class constructor, 135 GivePlayerOptions method, 135 MoveOption class, 132 Option interface, 131 QuitOption class, 134 RunGame method, 136 virtual methods, 127 pop method, 178 PrintHello function, 50 PrintOptions method, 188 PrintSum function, 50 Programming paradigms functions, 2 generic programming, 3 OOP, 2 Pure virtual methods, 130 push method, 178 R Recursion, 172 Relational operators equality operators, 22 greater-than operators, 23 less-than operators, 23 Right hift (>>) operator, 32 Room::EvaluateInput method, 188 RunGame method, 136 RunGameThread function, 268 S Saving and loading game progress Game Class constructor, 260 261 Player OnLoad method, 262 Player OnSave method, 261 262 Room class, 263 264 Room OnSave and Room OnLoad, 264 RunGame method, 259 SerializationManager class ClearSave method, 257 GetSerializable methods, 255 Load method, 256 RegisterSerializable method, 255 RemoveSerializable method, 255 Save method, 255 Serializable class, 257 Updated main function, 258 WelcomePlayer method, 260 Semantics move assignment, 143 Player class, 142 rvalue reference, 142 Serializable class, 258 SerializableManager Save method, 255 Serialization, 253 254 SerializationManager class method, 257 SerializationManager ClearSave method, 257 SerializationManager Load method, 256 Single threaded memory allocator, 231 Stack frames, 226 Standard template library (STL), 147 array and list memory layouts, 163 array template container, 157 C-style, 157 elements finding array, 158 binary search tree Find method, 173 head node, 172 if statement, 173 iteration, 173 recursion, 172 TreeNode class, 172 173 visual representation, 171 172
296 Index Standard template library (STL) (cont.) bitsets altering bits, 183 constructors, 182 creation, 181 definition, 181 flip method, 184 query, 182 reset method, 184 set method, 184 size method, 182 catching exceptions, 176 handle exceptions, 175 hash map, 173 ListNode class, 164 for loop function, 166 OurList function, 165 list::sort method, 167 list syntax, 166 list vs. vector, 166 map container, 170 pair templates, 170 queue container, 178 set container, 169 sorting arrays and vectors, 160 stack container, 177 Text Adventure game (see Text Adventure game) throwing exceptions, 175 unordered_map, 174 175 unordered_set, 174 vector class insert and erase methods, 159 loops, 160 specialization, 159 static_assert method, 204 STL string class basic_string aliases, 147 basic_string capacity methods, 149 basic_string modifiers, 149 constructors, 148 find_first_of method, 153 find method, 152 iterators auto with for loop, 152 constant iterators, 151 decrement string, 150 range-based for loop, 151 152 setprecision modifier, 154 stringstream modifiers, 154 strcat string, 45 strcmp string, 44 strcpy string, 45 String literal, 43 strlen string, 44 Subtraction operator, 18 Sword class, 190 T Template programming assert vs. static_assert, 204 compile vs. runtime time compilation, 201 const vs. constexpr, 203 EventManager class AttachEvent method, 212, 214 destructor, 213 DetachEvent method, 212, 214 Event class, 208 EventHandler interface, 208 friend function, 213 member variables and methods, 210 RegisterEvent method, 211, 214 SendEvent and SendEventToHandler method, 212 SendEventToHandler function, 214 static_assert code, 215 QuitEvent evaluate method, 218 game class updation, 218 HandleEvent method, 220 RunGame method, 219 SDBM hash algorithm, 216 Singleton class creation, 205 EventManager class, 206, 208 m_instance variable, 206 Text adventure game, 46 AddItem, 192 AttackEnemyOption class, 193 AttackEnemyOption::Evaluate method, 194
297 Chest class, 189 dynamic and static options, 187 Enemy class, 193 EvaluateInput method, 136 Game Class constructor, 135 Game constructor, 195 GivePlayerOptions method, 135 HasWeapon method, 192 Item class, 190 JoiningRooms array, 186 map, 187 MoveOption class, 132 OpenChestOption::Evaluate nethod, 191 OpenChestOptions class, 191 Option interface, 131 Option storage, 186 Player class, 192 QuitOption class, 134 Room::EvaluateInput method, 188 STL array code, 185 Sword class, 190 updated Game class, 194 vector, 186 Thread creation, 268 _tmain function, 55 Try_lock method, 270 Turing complete, 202 U Unary operators arithmetic increment and decrement, 33 negative operator, 33 plus operator, 33 Logical Not, 34 ~ operator, 35 unique_ptr, 231 Unlock method, 270 V vector class, 164 vector::push_back method, 188 Virtual methods, 127 Visitable class, 249 Visitor objects, 249 Visual Studio IDE, 100 W WelcomePlayer method, 55, 260 WindowsEnemy class, 282 X, Y, Z Xcode, 58