add inheritance
This commit is contained in:
parent
78b8da2649
commit
0746a9affd
24
class.cpp
24
class.cpp
@ -1,4 +1,5 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -26,10 +27,31 @@ void GameCharacter::takeDamage(int damage) {
|
||||
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";
|
||||
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";
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user