Last active
October 22, 2021 09:25
-
-
Save rugbyprof/dc3b7dbd29b3f8355ba91fa27d27bb27 to your computer and use it in GitHub Desktop.
Single Inheritance Example
This file contains 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> | |
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