Download this source file


// Example of using temporary objects with iostreams.  This idea comes from
// Jerry Schwarz, the creator of the initial iostream classes.  (Reference
// fround in B. Eckle's "Thinking in C++", Prentice Hall, 1995, pp 248-ff.)
// He calls the technique "Effectors".  The basic idea is to create a
// manipulator that take arguments as simple class whose constructor take
// arguments, and which has an inserter or extractor (or both).
//
// Written by Wayne Pollock, Tampa FL, 2000.

#include <cctype>
#include <iostream>
#include <cstring>

using namespace std;

// This manipulator will output a string with all lowercase
// letters translated into uppercase:

class upcase
{
public:
   upcase ( const char* s )
   {  int l = strlen( s );
      rep = new char[l + 1];
      strcpy( rep, s );
      for ( int i=0; i<l; ++i )
         rep[i] = toupper( rep[i] );
   }

   ~upcase () { delete[] rep; }
	
   friend ostream& operator<< ( ostream&, upcase& );

private:
   char* rep;
};

ostream& operator<< (ostream& out, upcase& s)
{   return out << s.rep; }


int main ()
{
   char test[] = "This is only a test! 1...2...3";

   cout << "Test string:\n\t" << test << endl;
   cout << "Uppercase version of test string:\n\t"
        << upcase(test) << endl;
   return 0;
}

#ifdef COMMENTED_OUT
     Output of above program:
Test string:
	This is only a test! 1...2...3
Uppercase version of test string:
	THIS IS ONLY A TEST! 1...2...3
#endif