Decorator Help

This is a discussion on Decorator Help within the C++ Programming forums, part of the General Programming Boards category; Can somebody explain how this code works? Like main() and such. Code: #include <string> #include <iostream> using namespace std; class ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023

    Decorator Help

    Can somebody explain how this code works? Like main() and such.

    Code:
    #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;
    }
    Output:
    <B>A party? For me? Thanks!</B>
    <I><B>A party? For me? Thanks!</B></I>
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I'll be the tutorials here at cprogramming can.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Read up on

    1. Constructors.
    2. Inheritance
    3. Virtual member functions

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    highly recommend

    Accelerated C++: Practical Programming by Example by Andrew Koenig and Barbara E. Moo

    Kuphryn

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by Dante Shamest
    Read up on

    1. Constructors.
    2. Inheritance
    3. Virtual member functions
    I know about all those things.

    What I don't get is why apparently .getHTML() is getting called twice in the last line. What's happening?

    And further more the following line only calls .getHTML() twice, because of something to do with copy constructors:

    cout << BoldParagraph(BoldParagraph(p)).getHTML() << endl;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    BoldParagraph is-a Paragraph, which allows it to be passed to the ItalicParagraph constructor. You call ItalicParagraph::getHtml which internally calls Paragraph::getHtml

  7. #7
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Quote Originally Posted by King Mir
    What I don't get is why apparently .getHTML() is getting called twice in the last line.
    ItalicParagraph's getHTML() adds <I></I> around BoldParagraphy's getHTML() which adds <B></B> around Paragraph's getHTML() which is "A party? For me? Thanks!"

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    I get it now. Thanks. I simmply missed the fact that BoldParagraph and ItalicParagraph call the wrapped Paragraph's .getHTML.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21