Fantasy character hierarchy

Report a typo

You are designing a fantasy role-playing game. Create a hierarchy of character classes using inheritance in C++. You already have a base class Character , and you need to derive classes like Wizard, Warrior, and Rogue.

Each derived class should have unique abilities and attributes:

  • character type (wizard, warrior, rogue) - This is the same attribute for all (only the default value differs).

  • signature action (Wizard - spell, Warrior - sword, and Rogue - drinking). The method must be reassigned from the base class.

For example:
- The wizard's method prints: "My superpower is spell";
- The warrior's method prints: "My superpower is sword";
- The rogue's method prints: "My superpower is drinking";

Here is the code for the Character class:

class Character{
private:
    std::string name;
    int age;
    std::string charType;
    
public:
    std::string printName();
    int printAge();
    std::string  printType();
    void printSuperPower();

    void setName(std::string n);
    void setAge(int a);

};

You need to implement 3 classes with two attributes: printSuperPower and setCharType

Sample Input 1:

Gandalf 200 w

Sample Output 1:

My name is Gandalf
I am a wizard
My age is 200 years
My superpower is spell

Sample Input 2:

Aragorn 35 m

Sample Output 2:

My name is Aragorn
I am a warrior
My age is 35 years
My superpower is sword

Sample Input 3:

Legolas 2931 r

Sample Output 3:

My name is Legolas
I am a rogue
My age is 2931 years
My superpower is drinking
Write a program in C++





Create a free account to access the full topic