VasilM

oop_decorating

May 13th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>;
  2. using namespace std;
  3.  
  4. class Win {
  5. public:
  6.     virtual void draw() = 0;
  7.     virtual ~Win() { cout<<"Win deleted!\n"; }
  8. };
  9.  
  10. class TextWin: public Win {
  11. private:
  12.     int width, height;
  13. public:
  14.     TextWin( int width, int height):width(width), height(height) {}
  15.     void draw() {
  16.         cout <<"TextWin: "<< width <<", "<< height << endl;
  17.     }
  18. };
  19.  
  20. class Decorator: public Win {
  21. private:
  22.     Win* window;
  23. public:
  24.     Decorator( Win* window ):window(window) {}
  25.     void draw() {
  26.         window->draw();
  27.     }
  28.     ~Decorator() { delete window; cout<<"Decorator deleted! "; }
  29. };
  30.  
  31. class BorderDecorator: public Decorator {
  32. public:
  33.     BorderDecorator( Win* window ): Decorator( window ) {}
  34.     void draw() {
  35.         cout << "Bordered ";
  36.         Decorator::draw();
  37.     }
  38.     ~BorderDecorator() { cout<<"BorderDecorator deleted! "; }
  39. };
  40.  
  41. class ScrollDecorator: public Decorator {
  42. public:
  43.     ScrollDecorator( Win* window ): Decorator( window ) {}
  44.     void draw() {
  45.         cout << "Scrolled ";
  46.         Decorator::draw();
  47.     }
  48.     ~ScrollDecorator() { cout<<"ScrollDecorator deleted! "; }
  49. };
  50.  
  51. int main() {
  52.     Win * a = new TextWin( 80, 15 ),
  53.         * b = new BorderDecorator( new TextWin(60,25)),
  54.         * c = new ScrollDecorator( new TextWin(70,24)),
  55.         * d = new BorderDecorator( new ScrollDecorator( new TextWin(80,243)));
  56.     a->draw();
  57.     b->draw();
  58.     c->draw();
  59.     d->draw();
  60.     cout <<"deleting a:\n"; delete a;
  61.     cout <<"deleting b:\n"; delete b;
  62.     cout <<"deleting c:\n"; delete c;
  63.     cout <<"deleting d:\n"; delete d;
  64.    
  65.     system("PAUSE");
  66.     return EXIT_SUCCESS;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment