Lesson 6: Object-Oriented Programming
Activity 23: Creating Game Characters
Create a Character class that has a public method moveTo that prints Moved to position:
class Character { public: void moveTo(Position newPosition) { position = newPosition; std::cout << “Moved to position “ << newPosition.positionIdentifier << std::endl; } private: Position position; };
Create a struct named Position:
struct Position { // Fields to describe the position go here std::string positionIdentifier; };
Create two classes Hero and Enemy that are derived from the class Character:
// Hero inherits publicly from Character: it has // all the public member of the Character class. class Hero : public Character { }; // Enemy inherits publicly from Character, like Hero class Enemy : public Character { };
Create a class Spell with the constructor that prints the name of the person casting the spell:
class Spell { public: Spell(std::string name) : d_name(name) {} ...