add classes

This commit is contained in:
Kishan Takoordyal 2020-11-13 14:56:58 +04:00
parent 5b40a2a16c
commit 78b8da2649
2 changed files with 35 additions and 0 deletions

BIN
class Executable file

Binary file not shown.

35
class.cpp Normal file
View File

@ -0,0 +1,35 @@
#include <iostream>
using namespace std;
class GameCharacter {
public:
int maxHealth, currentHealth, attack, defense;
GameCharacter(int, int, int);
void takeDamage(int);
};
GameCharacter::GameCharacter(int h, int a, int d) {
maxHealth = h;
currentHealth = h;
attack = a;
defense = d;
}
void GameCharacter::takeDamage(int damage) {
damage = attack - defense;
if (defense < 0) {
damage = 0;
}
currentHealth -= damage;
}
int main() {
GameCharacter character = GameCharacter(100, 20, 10);
cout << "Health before taking damage: " << character.currentHealth << "\n";
character.takeDamage(5);
cout << "Health after taking damage: " << character.currentHealth << "\n";
}