Video Coming Soon...

Created by Zed A. Shaw Updated 2026-06-19 17:00:40

56: Introducing classes

This exercise is pending. Quick notes about this exercise:

The Code

First, take the Person example and make it into a class:

View Source file ex56a.cpp Only

#include <fmt/core.h>
#include <chrono>
#include <thread>
#include <string>
#include <memory>

using std::shared_ptr, std::make_shared;

class Person {
public:
  std::string name;
  int age;

  void talk() {
    fmt::println("I am {} and I am {} years old.",
        name, age);
  }
};

int main(int argc, char* argv[]) {
  Person zed{"Zed", 51};
  Person mary{"Mary", 28};

  zed.talk();
  mary.talk();

  // or using shared_ptr
  auto zed_ptr = make_shared<Person>("Zed", 51);
  auto mary_ptr = make_shared<Person>("Mary", 28);

  zed_ptr->talk();
  mary_ptr->talk();
}

Also demonstrate how you can do the same thing in a struct, and how functions on structs/classes can take arguments as well:

View Source file ex56b.cpp Only

#include <fmt/core.h>
#include <chrono>
#include <thread>
#include <string>

struct Person {
  std::string name;
  int age;

  void talk() {
    fmt::println("I am {} and I am {} years old.",
        name, age);
  }

  void walk(int distance) {
    fmt::println("{} walks {} freedom units.",
        name, distance);
  }
};

int main(int argc, char* argv[]) {
  Person zed{"Zed", 51};
  Person mary{"Mary", 28};

  zed.talk();
  mary.talk();

  // make them walk
  zed.walk(100);
  mary.walk(10);
}

We use this to improve the combat engine some more.

The Breakdown

line of code
Description.

The Discussion

Blah blah.

Structs Too

do this one as a diff

See how I did it.View Source file ex56b.cpp Only

#include <fmt/core.h>
#include <chrono>
#include <thread>
#include <string>

struct Person {
  std::string name;
  int age;

  void talk() {
    fmt::println("I am {} and I am {} years old.",
        name, age);
  }

  void walk(int distance) {
    fmt::println("{} walks {} freedom units.",
        name, distance);
  }
};

int main(int argc, char* argv[]) {
  Person zed{"Zed", 51};
  Person mary{"Mary", 28};

  zed.talk();
  mary.talk();

  // make them walk
  zed.walk(100);
  mary.walk(10);
}

Updating the Engine

this is a diff of the previous as well

See how I did it.View Source file ex56.cpp Only

#include <fmt/core.h>
#include <string>
#include <random>
#include <memory>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <ranges>
#include <functional>

using std::shared_ptr, std::make_shared, std::string;

std::random_device RNG;
std::mt19937 GENERATOR(RNG());

int random_number(int from, int to) {
  std::uniform_int_distribution rand(from, to);
  return rand(GENERATOR);
}

struct Combatant {
  string name;
  int damage{0};
  int hp{0};
  bool dead{false};

  void fight(shared_ptr<Combatant> defender) {
    int damage = random_number(0, this->damage);

    if(damage > 0) {
      defender->hp -= damage;
      fmt::println("{} hit {} for {}. {} now has {} hp",
          this->name, defender->name, damage,
          defender->name, defender->hp);
    } else {
      fmt::println("{} missed {}!",
          this->name, defender->name);
    }

    if(defender->hp <= 0) {
      fmt::println("{} died. {} wins! {} has {} HP",
          defender->name, this->name,
          this->name, this->hp);
    }
  }
};

using ArenaID = int;
using Arena = std::unordered_map<ArenaID, shared_ptr<Combatant>>;
using CombatQueue = std::vector<size_t>;

void fill_arena(Arena& arena, int combatants) {
  using namespace std::ranges;

  for(auto arena_id : views::iota(0, combatants)) {
    string name = fmt::format("Giant Rat #{}", arena_id);
    int hp = random_number(5,20);
    int damage = random_number(2,10);
    auto fighter = make_shared<Combatant>(name, damage, hp);
    arena.try_emplace(arena_id, fighter);
  }
}

bool combat_active(Arena& arena, shared_ptr<Combatant> player) {
  return !arena.empty() && player->hp > 0;
}

CombatQueue queue_combat(Arena& arena) {
  using namespace std::ranges;

  auto queue = arena | views::keys | to<CombatQueue>();

  shuffle(queue, GENERATOR);

  return queue;
}

void run_combat(shared_ptr<Combatant> player, shared_ptr<Combatant> who) {
  bool player_turn = random_number(0,1);

  if(player_turn) {
    who->fight(player);
  } else {
    player->fight(who);
  }

  // now how to remove when dead
  if(who->hp <= 0) {
    who->dead = true;
  }
}

void cull_the_dead(Arena& arena) {
  using namespace std::ranges;

  auto is_dead = [&](auto& tuple) -> bool { return tuple.second->dead; };

  auto dead = arena | views::filter(is_dead) | views::elements<0> | to<CombatQueue>();

  for(auto id : dead) {
    arena.erase(id);
  }
}

int main(int argc, char* argv[]) {
  using namespace std::ranges;

  Arena arena;
  auto player = make_shared<Combatant>("Player", 20, 50);

  fill_arena(arena, 10);

  // now with multiple combatants we have to keep going until the player dies
  while(combat_active(arena, player)) {
    auto whos_next = queue_combat(arena);

    for_each(whos_next, [&](auto enemy_id) {
      run_combat(player, arena[enemy_id]);
    });

    cull_the_dead(arena);
  }

  if(arena.empty()) {
    fmt::println("The Player Wins! You defeated all of the rats.");
  } else {
    fmt::println("You Died! The Rats Win.");
  }
}

Further Study

Previous Lesson Next Lesson

Register for Learn C++ the Hard Way

Register to gain access to additional videos which demonstrate each exercise. Videos are priced to cover the cost of hosting.