Skip to content

Instantly share code, notes, and snippets.

@rugbyprof
Last active October 22, 2021 09:25
Show Gist options
  • Save rugbyprof/dc3b7dbd29b3f8355ba91fa27d27bb27 to your computer and use it in GitHub Desktop.
Save rugbyprof/dc3b7dbd29b3f8355ba91fa27d27bb27 to your computer and use it in GitHub Desktop.
Single Inheritance Example
#include <iostream>
using namespace std;
class Character {
protected:
string name; // character name
string weapon; // type of weapon
// plus more
public:
void setName(string n) {
cout << "Setting name using Character class method.\n";
this->name = n;
}
};
class Archer : public Character {
private:
int arrow_count;
public:
Archer() {
cout<<"Derived class constructor setting weapon to bow and arrows to 10.\n";
this->weapon = "bow";
this->arrow_count = 10;
}
void ShootArrow() {
cout << "Shooting arrow!\n";
this->arrow_count--;
}
};
int main() {
Archer A1;
A1.setName("Boldivar");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment