Created
March 9, 2015 13:15
-
-
Save Tasemu/659c8035890ba19ecd41 to your computer and use it in GitHub Desktop.
learning c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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