GregLeck

Untitled

Feb 17th, 2022
850
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct Character
  6. {
  7.     Character();
  8.  
  9.     void PrintHealth();
  10.  
  11.     string Name;
  12.     float Health;
  13. };
  14.  
  15. int main()
  16. {
  17.     // Create PtrToChar with default constructor
  18.     // Created at run-time and placed on the heap,
  19.     // as opposed to compile-time when placed on stack.
  20.     for (int i = 0; i < 10; i++)
  21.     {
  22.         Character* PtrToChar = new Character();
  23.         cout << PtrToChar->Name << endl;
  24.         PtrToChar->PrintHealth();
  25.         delete PtrToChar;
  26.     }
  27.  
  28.     system("pause");
  29. }
  30.  
  31. Character::Character()
  32. {
  33.     Name = "Default Name";
  34.     Health = 100.f;
  35. }
  36.  
  37. void Character::PrintHealth()
  38. {
  39.     cout << "Health = " << Health << endl;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment