Pages

Jun 5, 2011

COUT Class code


#include <stdio.h>
#include<iostream>
using namespace std;
#define endl '\n'
class syp{
  ostream* ost;   // output stream  pointer
public:
  syp(ostream* is){     // accepting 'cout' as constructor argument
     ost = is;                 // assigning 'cout' to output stream ost
  }
 friend syp operator<<(const syp&, const char*); //(lft operand -> class obj,rht operand -> char*/int /double)
 friend syp operator<<(const syp&, const int);      // e.g.   syp<<8;
 friend syp operator<<(const syp& s, const double x);
 friend syp operator<<(const syp& s, const char c);
 };

syp operator<<(const syp& s, const char c){
s.ost->put(c);     // put (const char) is a function of ostream class
return s;              // return class syp object  's'
}
 syp operator<<(const syp& s, const char* os){
s.ost->write(os,strlen(os));    // write (const char*, length) is a function of ostream class
return s;
 }
 syp operator<<(const syp& s, const int x){
s.ost->operator<<(x);           // operator<<(const int) is an operator of ostream class
return s;
 }
 syp operator<<(const syp& s, const double x){
s.ost->operator<<(x);           //operator<<(const double) is an operator of ostream class
return s;
 }