slice.cpp

URL: http://147.64.242.52/~bennett/class/cmsc2040/fall2026/notes/inherit/code/preClass/slice.cpp
 
#include <iostream>
#include "NewPlayerT.h"

using namespace std;

class BarbarianT : public FighterT  {
    public:
       BarbarianT(string newName):
           FighterT{newName + " barbarian " } 
       {};

       string Role() const override {
           return "Barbarian";
       }

       int Rage() const {
          return rage;
       }

       int Chaos() const {
          return chaos;
       }

    private: 
       int rage{999};
       int chaos{-10};
};

void TellAboutPlayer(NewPlayerT *  player);

int main() {
    FighterT fighter("Fred");
    WizardT wizard("Walt");
    MonkT monk("Morty");
    BarbarianT barbarian("Arnold");

    // slice
    cout << "About to slice" << endl;
    FighterT newFighter = barbarian;
    cout  << "The barbarian thinks he is a " << newFighter.Role() << endl;

    // can't do this.
    //BarbarianT newBarbarian = fighter;

    cout << endl << endl;
    cout << "The players tell us about themselves" << endl;
    TellAboutPlayer(&fighter);
    TellAboutPlayer(&wizard);
    TellAboutPlayer(&monk);
    TellAboutPlayer(&barbarian);


    return 0;
}

void TellAboutPlayer(NewPlayerT *  player){
    cout << "This is " << player->Name() << endl;
    cout << "They are a " << player->Role() << endl;

    // cout << "\t Magic: " << player->Magic() << endl;

    //FighterT * fighterPointer = dynamic_cast<FighterT *> (player);
    FighterT * fighterPointer = static_cast<FighterT *> (player);

    if (nullptr != fighterPointer) {
        cout << "\t Strength: " << fighterPointer->Strength() << endl;
    }

    //BarbarianT * barbarianPointer = dynamic_cast<BarbarianT * >(player);
    BarbarianT * barbarianPointer = static_cast<BarbarianT * >(player);
    cout << "\t Rage: " << barbarianPointer->Rage() << endl;
    cout << "\t Chaos: " << barbarianPointer->Chaos() << endl;

    cout << endl;
}