Skip to content

Instantly share code, notes, and snippets.

@Tasemu
Created March 9, 2015 13:15
Show Gist options
  • Save Tasemu/659c8035890ba19ecd41 to your computer and use it in GitHub Desktop.
Save Tasemu/659c8035890ba19ecd41 to your computer and use it in GitHub Desktop.
learning c++
#include <iostream>
#include <string>
using namespace std;
class Enemy {
protected:
int attackPower;
int hp;
public:
void setAttackPower (int x) {
attackPower = x;
}
int getHP () {
return hp;
}
void setHP (int x) {
hp = x;
}
void takeDamage(int x) {
hp -= x;
cout << "new hp: " << hp << endl;
}
bool isAlive () {
return hp != 0;
}
};
class Samurai: public Enemy {
public:
void attack (Enemy *target) {
cout << "BANZAI! -" << attackPower << endl;
target->takeDamage(attackPower);
}
};
int main () {
Samurai kenshin;
kenshin.setAttackPower(50);
kenshin.setHP(100);
Samurai gin;
gin.setAttackPower(30);
gin.setHP(100);
while (gin.isAlive()) {
kenshin.attack(&gin);
};
cout << "Gin is dead!" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment