Site Tools


c_decorator
  #include <QtCore/QCoreApplication>
  #include "decorator.h"
  #include <iostream>
  
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
      Drinks *order = new Moacha(new Soy(new Coffee));
      std::cout << order->coast() << std::endl;
      std::cout << order->getDescription() << std::endl;
  
      return 1;
  }
  #ifndef DECORATOR_H
  #define DECORATOR_H
  
  #include <string>
  using namespace std;
  
  class Drinks{
  public:
      string description;
      virtual double coast(){};
      virtual string getDescription(){};
  };
  
  class DecoratorDrinks : public Drinks
  {
  private:
      string description;
  public:
      virtual double coast(){};
      virtual string getDescription(){};
      DecoratorDrinks(){};
  };
  
  class Moacha : public DecoratorDrinks{
  private:
      Drinks* before;
  public:
      Moacha(Drinks*);
      string getDescription();
      double coast();
  };
  
  class Whip : public DecoratorDrinks{
  private:
      Drinks* before;
  public:
      Whip(Drinks*);
      string getDescription();
      double coast();
  };
  
  class Soy :public DecoratorDrinks{
  private:
      Drinks* before;
  public:
      Soy(Drinks*);
      string getDescription();
      double coast();
  };
  
  class Coffee : public Drinks {
  public:
      Coffee();
      double coast();
      string getDescription();
  };
  
  #endif // DECORATOR_H
  #include "decorator.h"
  
  
  
  double Moacha::coast(){
      return before->coast() + 0.10;
  }
  
  string Moacha::getDescription(){
      return before->getDescription() + ", Moacha";
  }
  
  Moacha::Moacha(Drinks* inDrink){
      before = inDrink;
  }
  
  double Whip::coast(){
      return before->coast() + 0.20;
  }
  
  string Whip::getDescription(){
      return before->getDescription() + ", Whip";
  }
  
  Whip::Whip(Drinks* inDrink){
      before = inDrink;
  }
  
  double Soy::coast(){
      return before->coast() + 0.40;
  }
  
  string Soy::getDescription(){
      return before->getDescription() + ", Soy";
  }
  
  Soy::Soy(Drinks* inDrink){
      before = inDrink;
  }
  
  double Coffee::coast(){
      return 1.99;
  }
  
  Coffee::Coffee(){
      description = "Normal Coffe";
  }
  
  string Coffee::getDescription(){
      return description;
  }

вывод

2.49
Normal Coffe, Soy, Moacha
You could leave a comment if you were logged in.
c_decorator.txt · Last modified: 2013/02/28 13:23 by konovalov

Page Tools