Skip to content

Instantly share code, notes, and snippets.

@todocono
Last active July 19, 2023 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todocono/311e13eabfe21485753a3b031824e858 to your computer and use it in GitHub Desktop.
Save todocono/311e13eabfe21485753a3b031824e858 to your computer and use it in GitHub Desktop.
Lecture 5 examples, including inheritance, public, private, protected, and new
//Lecture 5 - BIT2400
//Example 3 - Inheritance & protected
//Exercise: Watch https://www.youtube.com/watch?v=jsNE1yGItx0 and
//make bullets have a different hit value depending on speed
#include <iostream>
#include <string>
using namespace std; //this is OK for our class only
class Player; //forward declaration (because I need it inside of Enemy and it doesn't exist yet)
class Enemy {
private:
int hp;
string name;
protected: //note that now attackPoints are protected instead of private (so they are inherited)
int attackPoints;
public:
void hit(Player& p);
Enemy() {
hp = 0;
name = "Dr No";
attackPoints = 10;
}
};
class Player {
private:
int hp;
public:
string name;
int getHp(); //this is similar to the prototype declaration of a function
void gotAttacked(int points);
friend void resetHp(Player& p);
friend void Enemy::hit(Player& p); //I could make the whole class friend
friend class Bullet; // Friendship is a non-transitive relationship, meaning that it does not pass from one class to another through inheritance.
Player() {
name = "Gene Ric Name";
hp = 100; //don't leave uninitialized variables!
}
};
class Bullet : public Enemy { //not to be confused with other public enemies
private:
float speed;
static int qtyBulletsShot;
public:
float getSpeed() {
return speed;
}
void shoot();
void hit(Player& p);
Bullet(float s) {
speed = s;
qtyBulletsShot++;
}
~Bullet() {
cout << "-bullet destroyed-" << endl;
}
static int howManyBullets() { //this function doesn't belong technically to the object but to the class
return qtyBulletsShot;
}
};
int Player::getHp() { //this "getter" reads the private health points and shares it, but doesn't allow others to write it
return hp;
}
void Player::gotAttacked(int points) { //this attack function is an indirect way of modifying hp
if ((*this).getHp() > 0) { //note the parenthesis around the *
this->hp = this->hp - points; //a way to avoid the (* ) is to use ->
}
return;
}
void Enemy::hit(Player& p) {
p.hp -= attackPoints;
}
void resetHp(Player& p) {
p.hp = 100;
}
void Bullet::shoot() {
this->speed = 3.14; //a great place to decide how fast bullets will go
}
void Bullet::hit(Player& p) {
//you need to fill in code here
return;
}
int Bullet::qtyBulletsShot = 0; //I need to initialize static variables before using them
int main() {
Player p1;
Enemy e1;
cout << "The name of the Player 1 assigned by the constructor was: " << p1.name;
p1.name = "PlayerOgre12345";
cout << ", but it changed to" << p1.name << endl << endl;
cout << "Health Points cannot be accessed directly, only through the getter. Now getHp() gives: " << p1.getHp() << endl << endl;
p1.gotAttacked(5);
cout << "After the attack, health is: " << p1.getHp() << endl << endl;
resetHp(p1);
cout << "After resetting HP: " << p1.getHp() << endl << endl;
e1.hit(p1);
cout << "Enemy hits p1, so now HP is: " << p1.getHp() << endl << endl;
for (int i = 0; i < 5; i++) {
Bullet* b = new Bullet(2.39);
b->hit(p1);
cout << "Shooting bullet number " << i << ", leaving p1 with health: " << p1.getHp() << endl;
delete b;
}
cout << "Total of bullets shot is: " << Bullet::howManyBullets() << endl << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment