C++/Overload/Inserter

Материал из C\C++ эксперт
Перейти к: навигация, поиск

An inserter can be used to output data in any form

#include <iostream>
using namespace std;
class MyClass {
  int x, y;
public:
  MyClass(int i, int j) { 
     x=i; 
     y=j; 
  }
  friend ostream &operator<<(ostream &stream, MyClass o);
};
ostream &operator<<(ostream &stream, MyClass o)
{
  register int i, j;
  for(i=0; i<o.x; i++)
    stream << "*";
  stream << "\n";
  for(j=1; j<o.y-1; j++) {
    for(i=0; i<o.x; i++)
      if(i==0 || i==o.x-1) stream << "*";
      else stream << " ";
    stream << "\n";
  }
  for(i=0; i<o.x; i++)
     stream << "*";
  stream << "\n";
  return stream;
}
int main()
{
  MyClass a(14, 6), b(30, 7), c(40, 5);
  cout << "Here are some:\n";
  cout << a << b << c;
  return 0;
}


Create a non-friend inserter.

#include <iostream>
using namespace std;
class MyClass {
public:
  int x, y;  // must be public
  MyClass() { 
     x = 0; 
     y = 0; 
  }
  MyClass(int i, int j) { 
     x = i; 
     y = j; 
  }
};
// An inserter for the MyClass class.
ostream &operator<<(ostream &stream, MyClass ob)
{
  stream << ob.x << ", " << ob.y << "\n";
  return stream;
}
int main()
{
  MyClass a(1, 1), b(10, 23);
  cout << a << b;
  return 0;
}


Output account info to a file using an inserter.

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
class account {
  int custnum;
  char name[80];
  double balance;
public:
  account(int c, char *n, double b) 
  {
    custnum = c;
    strcpy(name, n);
    balance = b;
  }
  friend ostream &operator<<(ostream &stream, account ob);
};
ostream &operator<<(ostream &stream, account ob) 
{
  stream << ob.custnum << " ";
  stream << ob.name << " " << ob.balance;
  stream << "\n";
  return stream;
}
int main()
{
  account  Rex(1011, "Joe", 12323.34);
  ofstream out("accounts", ios::out | ios::binary);
  if(!out) {
    cout << "Cannot open output file.\n";
    return 1;
  }
  out << Rex;
  out.close();
  return 0;
}


PhoneNumber inserter function

#include <iostream>
#include <cstring>
using namespace std;
class PhoneNumber {
public:
  char name[80];
  int areaCode;
  int prefix;
  int num;
  PhoneNumber(char *n, int a, int p, int nm){
    strcpy(name, n);
    areaCode = a;
    prefix = p;
    num = nm;
  }
};
// Display name and phone number.
ostream &operator<<(ostream &stream, PhoneNumber o)
{
  stream << o.name << " ";
  stream << "(" << o.areaCode << ") ";
  stream << o.prefix << "-" << o.num << "\n";
  return stream; // must return stream
}
int main()
{
  PhoneNumber a("T", 111, 555, 1234);
  PhoneNumber b("A", 312, 555, 5768);
  PhoneNumber c("T", 212, 555, 9991);
  cout << a << b << c;
  return 0;
}


This program draws right triangles

#include <iostream>
using namespace std;
class triangle {
  int height, base;
public:
  triangle(int h, int b) { height = h; base = b; }
  friend ostream &operator<<(ostream &stream, triangle ob);
};
// Draw a triangle.
ostream &operator<<(ostream &stream, triangle ob)
{
  int i, j, h, k;
  i = j = ob.base-1;
  for(h=ob.height-1; h; h--) {
    for(k=i; k; k--) 
      stream << " ";
    stream << "*";
    
    if(j!=i) {
      for(k=j-i-1; k; k--)
        stream << " ";
      stream << "*";
    }
    i--;
    stream << "\n";
  }
  for(k=0; k<ob.base; k++) stream << "*";
  stream << "\n";
  return stream;
}
int main()
{
  triangle t1(5, 5), t2(10, 10), t3(12, 12);
  cout << t1;
  cout << endl << t2 << endl << t3;
  return 0;
}


Use a friend inserter for objects of type MyClass.

#include <iostream>
using namespace std;
class MyClass {
  int x, y;
public:
  MyClass() { 
     x = 0; 
     y = 0; 
  }
  MyClass(int i, int j) { 
     x = i; 
     y = j; 
  }
  friend ostream &operator<<(ostream &stream, MyClass ob);
};
ostream &operator<<(ostream &stream, MyClass ob)
{
  stream << ob.x << ", " << ob.y << "\n";
  return stream;
}
int main()
{
  MyClass a(1, 1), b(10, 23);
  cout << a << b;
  return 0;
}


Use friend function to operator: <<, >>

>" src="http://www.java2s.com/Code/CppImages/Usefriendfunctiontooperator.PNG">


#include <iostream>
using namespace std;
class factor {
  int num;   // number
  int lfact; // lowest factor
public:
  factor(int i);
  friend ostream &operator<<(ostream &stream, factor ob);
  friend istream &operator>>(istream &stream, factor &ob);
};
factor::factor(int i)
{
  int n;
  num = i;
  for(n=2; n < (i/2); n++)
    if(!(i%n)) break;
  if(n<(i/2)) lfact = n;
  else lfact = 1;
}
istream &operator>>(istream &stream, factor &ob)
{
  stream >> ob.num;
  int n;
  for(n=2; n < (ob.num/2); n++)
    if(!(ob.num%n)) break;
  if(n < (ob.num/2)) 
     ob.lfact = n;
  else 
  ob.lfact = 1;
  return stream;
}
ostream &operator<<(ostream &stream, factor ob)
{
  stream << ob.lfact << " is lowest factor of ";
  stream << ob.num << "\n";
  return stream;
}
int main()
{
  factor o(32);
  cout << o;
  cin >> o;
  cout << o;
  return 0;
}