58 lines
1.2 KiB
C++
58 lines
1.2 KiB
C++
#include <iostream>
|
|
#include <vector>
|
|
|
|
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;
|
|
}
|
|
|
|
class Player: public GameCharacter {
|
|
public:
|
|
string name;
|
|
vector<string> inventory;
|
|
Player(string, int, int, int);
|
|
void addItem(string);
|
|
};
|
|
|
|
Player::Player(string n, int h, int a, int d):GameCharacter(h, a, d) {
|
|
name = n;
|
|
}
|
|
|
|
void Player::addItem(string item) {
|
|
inventory.push_back(item);
|
|
}
|
|
|
|
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\n";
|
|
|
|
Player p = Player("Kishan", 120, 30, 10);
|
|
|
|
p.addItem("boots");
|
|
cout << "Length of the inventory of " << p.name << ": " << p.inventory.size() << "\n";
|
|
}
|