Skip to content

Instantly share code, notes, and snippets.

@jbenner-radham
Created April 2, 2014 17:36
Show Gist options
  • Save jbenner-radham/9939043 to your computer and use it in GitHub Desktop.
Save jbenner-radham/9939043 to your computer and use it in GitHub Desktop.
Random C++ I was playing around with because I'm a noob.
/* g++ -std=c++0x -Wall [-static] <source> [-o <binary>] */
/* Now with awesome proper constructors and destructors. */
#include <iostream>
using namespace std;
class Hero
{
public:
int hero_hp;
Hero(int hp = 100) {
hero_hp = hp;
cout << "A hero has come to us!" << endl;
}
void takesDamage(int hp_lost) {
hero_hp = hero_hp - hp_lost;
cout << "Our hero takes " << hp_lost << " points of damage." << endl;
}
void isHealed(int hp_gained) {
hero_hp = hero_hp + hp_gained;
cout << "Our hero is healed for " << hp_gained << " points of health." << endl;
}
void currentHealth() {
cout << "Our hero currently has " << hero_hp << "HP." << endl;
}
void forCereal() {
cout << "For Cereal Dawg!" << endl;
Hero::oiVey();
}
~Hero() {
cout << "Our hero is going to party like it's 1999 for a bit, cya later!" << endl;
}
private:
void oiVey() {
cout << "Oi Vey!" << endl;
}
};
int main()
{
/*
//Hero json(155); // Instantiate with a custome argument vector.
Hero json; // Instantiate with the default int value.
json.currentHealth();
json.isHealed(5);
json.currentHealth();
json.takesDamage(3);
json.currentHealth();
//delete json; // You cannot delete an Object created without a new keyword.
*/
Hero* json = new Hero; // Using the "new" keyword must be invoked as a class pointer.
json->currentHealth();
json->isHealed(5);
json->currentHealth();
json->takesDamage(3);
json->currentHealth();
delete json; // You can delete Objects declared with the "new" keyword.
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment