topical media & game development

talk show tell print

professional-program-26-Decorator-Decorator.c

? / professional-program-26-Decorator-Decorator.c


  include <string>
  include <iostream>
  
  using namespace std;
  
  class Paragraph
  {
  public:
    Paragraph(const string& inInitialText) : mText(inInitialText) {}
          
    virtual string getHTML() const { return mText; }
  
  protected:
    string mText;        
  }; 
  
  class BoldParagraph : public Paragraph
  {
  public:
    BoldParagraph(const Paragraph& inParagraph) :
      Paragraph(""), mWrapped(inParagraph) {}
  
    virtual string getHTML() const {
      return "<B>" + mWrapped.getHTML() + "</B>";
    }
  
  protected:
    const Paragraph& mWrapped;
  };
  
  class ItalicParagraph : public Paragraph
  {
  public:
    ItalicParagraph(const Paragraph& inParagraph) :
      Paragraph(""), mWrapped(inParagraph) {}
  
    virtual string getHTML() const {
      return "<I>" + mWrapped.getHTML() + "</I>";
    }
  
  protected:
    const Paragraph& mWrapped;
  };
  
  int main(int argc, char** argv)
  {
    Paragraph p("A party? For me? Thanks!");
  
    // Bold
    cout << BoldParagraph(p).getHTML() << endl;
  
    // Bold and Italic
    cout << ItalicParagraph(BoldParagraph(p)).getHTML() << endl;
  } 
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.