| | | | | |

I/O Manipulators with Parameters - 1

  • Use
  • std::cout << Beep(10);  // output 10 "beep"s
    
  • Implementation -- non-generic
  • // the manipulator function class
    class Beep
    {
      private:
        int beeps_;
    
      public:
        // constructor takes one parameter -- the number of beeps
        Beep (int n) : beeps_(n)
        {}
    
        // overload of () takes one parameter -- the ostream
        void operator () (std::ostream& os) const
        {
          for (int i = 0; i < beeps_; ++i)
            os.put('\a');
        }
    };
    
    // overload of output operator for function class
    std::ostream& operator << (std::ostream& os, Beep b)
    {
      b(os);
      return os;
    }
    

| | Top of Page | 5. Function Classes & - 8 of 11