In this challenge, we will practice creating a header file for one of our classes.
Given we have the following class in a .cpp
file, move the declaration into a .h
file and use the cpp
file for implementation. Remember to use a header guard!
// Character.cpp
class Character {
public:
virtual void Attack(Character* Target){
Target->TakeDamage(BaseDamage);
};
virtual void TakeDamage(int Amount){
Health -= Amount;
};
bool isDead() { return Health <= 0; }
protected:
int BaseDamage { 75 };
int Health { 500 };
};
// Character.h
#pragma once
class Character {
public:
virtual void Attack(Character* Target);
virtual void TakeDamage(int Amount);
bool isDead();
protected:
int BaseDamage { 75 };
int Health { 500 };
};
// Character.cpp
#include "Character.h"
void Character::Attack(Character* Target) {
Target->TakeDamage(BaseDamage);
};
void Character::TakeDamage(int Amount) {
Health -= Amount;
};
bool Character::isDead() { return Health <= 0; }
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way