// main.cpp // PolymorphicArray // Created by Robert Metcalfe on 29/05/13 ... Abstract base class, two subclasses, polymorphic array // Copyright (c) 2013 Robert Metcalfe. All rights reserved. using namespace std; #include class Cmd { public: virtual void showIt() { std::cout << "Nothing to do" << std::endl; }; void execute(Cmd* ourcp) { ourcp->showIt(); }; }; class CmdMessage: public Cmd { public: char* mymsg; CmdMessage(char* mymessage) { mymsg = mymessage; }; void showIt() { std::cout << mymsg << std::endl; }; }; class CmdRepeat: public Cmd { public: Cmd* thecmd; int timesToDo; CmdRepeat(Cmd* mycmd, int n) { thecmd = mycmd; timesToDo = n; }; void showIt() { for (int i=0; ishowIt(); }; }; int main(int argc, char** argv) { CmdMessage wakeUp((char *)"Wake up"); CmdMessage getOutOfBed((char *)"Get out of bed"); CmdMessage sitDownForBreakfast((char *)"Sit down for breakfast"); CmdMessage eatYourGreens((char *)"Eat your greens"); CmdRepeat repetition(&eatYourGreens, 5); Cmd* thecmds[4] = {&wakeUp, &getOutOfBed, &sitDownForBreakfast, &repetition}; for (int i=0; i<4; i++) thecmds[i]->showIt(); return 0; }