C++/Overload/Inserter Extractor

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

Demonstrate a custom inserter and extractor for objects of type ThreeD.

  
#include <iostream>
using namespace std;
class ThreeD {
  int x, y, z;
public:
  ThreeD(int a, int b, int c) { x = a; y = b; z = c; }
  // Make the inserter and extractor friends of ThreeD.
  friend ostream &operator<<(ostream &stream, const ThreeD &obj);
  friend istream &operator>>(istream &stream, ThreeD &obj);
};
// ThreeD inserter. Display the X, Y, Z coordinates.
ostream &operator<<(ostream &stream, const ThreeD &obj)
{
  stream << obj.x << ", ";
  stream << obj.y << ", ";
  stream << obj.z << "\n";
  return stream;
}
// ThreeD extractor. Get three-dimensional values.
istream &operator>>(istream &stream, ThreeD &obj)
{
  stream >> obj.x >> obj.y >> obj.z;
  return stream;
}
int main()
{
  ThreeD td(1, 2, 3);
  cout << "The coordinates in td: " << td << endl;
  cout << "Enter new three-d coordinates: ";
  cin >> td;
  cout << "The coordinates in td are now: " << td << endl;
  return 0;
}


Non-member functions are used to create custom inserters for three_d objects and to overload + for int + three_d.

  
#include <iostream>
using namespace std;
class three_d {
  int x, y, z;
public:
  three_d() { x = y = z = 0; }
  three_d(int i, int j, int k) { x = i; y = j; z = k; }
  three_d operator+(three_d rh_op);
  three_d operator+(int rh_op);
  three_d operator-(three_d rh_op);
  three_d operator=(three_d rh_op);
  bool operator==(three_d rh_op);
  three_d operator-();
  friend ostream &operator<<(ostream &strm, three_d op);
  friend three_d operator+(int lh_op, three_d rh_op);
};
three_d three_d::operator+(three_d rh_op)
{
  three_d temp;
  temp.x = x + rh_op.x;
  temp.y = y + rh_op.y;
  temp.z = z + rh_op.z;
  return temp;
}
three_d three_d::operator+(int rh_op)
{
  three_d temp;
  temp.x = x + rh_op;
  temp.y = y + rh_op;
  temp.z = z + rh_op;
  return temp;
}
three_d three_d::operator-(three_d rh_op)
{
  three_d temp;
  temp.x = x - rh_op.x;
  temp.y = y - rh_op.y;
  temp.z = z - rh_op.z;
  return temp;
}
three_d three_d::operator-()
{
  three_d temp;
  temp.x = -x;
  temp.y = -y;
  temp.z = -z;
  return temp;
}
three_d three_d::operator=(three_d rh_op)
{
  x = rh_op.x;
  y = rh_op.y;
  z = rh_op.z;
  return *this;
}
bool three_d::operator==(three_d rh_op)
{
  if( (x == rh_op.x) && (y == rh_op.y) && (z == rh_op.z) )
    return true;
  return false;
}
ostream &operator<<(ostream &strm, three_d op) {
  strm << op.x << ", " << op.y << ", " << op.z << endl;
  return strm;
}
three_d operator+(int lh_op, three_d rh_op) {
  three_d temp;
  temp.x = lh_op + rh_op.x;
  temp.y = lh_op + rh_op.y;
  temp.z = lh_op + rh_op.z;
  return temp;
}
int main()
{
  three_d objA(1, 2, 3), objB(10, 10, 10), objC;
  cout << "This is objA: " << objA;
  cout << "This is objB: " << objB;
  objC = -objA;
  cout << "This is -objA: " << objC;
  objC = objA + objB;
  cout << "objA + objB: " << objC;
  objC = objA - objB;
  cout << "objA - objB: " << objC;
  objC = objA + 10;
  cout << "objA + 10: " << objC;
  objC = 100 + objA;
  cout << "100 + objA: " << objC;
  if(objA == objB) 
     cout << "objA is equal to objB.\n";
  else 
     cout << "objA is not equal to objB.\n";
  return 0;
}


Overloading operator

  
#include <iostream>
#include <string.h>
using namespace std;
class String
{
   public:
      String();
       String(const char *const);
       String(const String &);
      ~String();
      char & operator[](int offset);
      char operator[](int offset) const;
      String operator+(const String&);
      void operator+=(const String&);
      String & operator= (const String &);
      friend ostream& operator<<( ostream& theStream,String& theString);
      int GetLen()const { return itsLen; }
      const char * GetString() const { return itsString; }
   private:
      String (int);
      char * itsString;
      unsigned short itsLen;
};
String::String(){
   itsString = new char[1];
   itsString[0] = "\0";
   itsLen=0;
}
String::String(int len)
{
   itsString = new char[len+1];
   for (int i = 0; i<=len; i++)
      itsString[i] = "\0";
   itsLen=len;
}
String::String(const char * const cString)
{
   itsLen = strlen(cString);
   itsString = new char[itsLen+1];
   for (int i = 0; i<itsLen; i++)
      itsString[i] = cString[i];
   itsString[itsLen]="\0";
}
String::String (const String & rhs)
{
   itsLen=rhs.GetLen();
   itsString = new char[itsLen+1];
   for (int i = 0; i<itsLen;i++)
      itsString[i] = rhs[i];
   itsString[itsLen] = "\0";
}
String::~String ()
{
   delete [] itsString;
   itsLen = 0;
}
String& String::operator=(const String & rhs)
{
   if (this == &rhs)
      return *this;
   delete [] itsString;
   itsLen=rhs.GetLen();
   itsString = new char[itsLen+1];
   for (int i = 0; i<itsLen;i++)
      itsString[i] = rhs[i];
   itsString[itsLen] = "\0";
   return *this;
}
char & String::operator[](int offset){
   if (offset > itsLen)
      return itsString[itsLen-1];
   else
      return itsString[offset];
}
char String::operator[](int offset) const
{
   if (offset > itsLen)
      return itsString[itsLen-1];
   else
      return itsString[offset];
}
String String::operator+(const String& rhs)
{
   int  totalLen = itsLen + rhs.GetLen();
   String temp(totalLen);
   int i, j;
   for (i = 0; i<itsLen; i++)
      temp[i] = itsString[i];
   for (j = 0; j<rhs.GetLen(); j++, i++)
      temp[i] = rhs[j];
   temp[totalLen]="\0";
   return temp;
}
void String::operator+=(const String& rhs)
{
   unsigned short rhsLen = rhs.GetLen();
   unsigned short totalLen = itsLen + rhsLen;
   String  temp(totalLen);
   int i, j;
   for (i = 0; i<itsLen; i++)
      temp[i] = itsString[i];
   for (j = 0, i = 0; j<rhs.GetLen(); j++, i++)
      temp[i] = rhs[i-itsLen];
   temp[totalLen]="\0";
   *this = temp;
}
ostream& operator<< ( ostream& theStream,String& theString)
{
     theStream << theString.itsString;
     return theStream;
}
int main()
{
    String theString("Hello world.");
    cout << theString;
  return 0;
}


Overload ostream and istream

  
#include <iostream>
#include <cstring>
using namespace std;
class inventory {
  char item[40];              // name of item
  int onhand;                 // number on hand
  double cost;                // cost of item
public:
  inventory(char *i, int o, double c)
  {
    strcpy(item, i);
    onhand = o;
    cost = c;
  }
  friend ostream &operator<<(ostream &stream, inventory ob);
  friend istream &operator>>(istream &stream, inventory &ob);
};
ostream &operator<<(ostream &stream, inventory ob)
{
  stream << ob.item << ": " << ob.onhand;
  stream << " on hand at $" << ob.cost << "\n";
  return stream;
}
istream &operator>>(istream &stream, inventory &ob)
{
  cout << "Enter item name: ";
  stream >> ob.item;
  cout << "Enter number on hand: ";
  stream >> ob.onhand;
  cout << "Enter cost: ";
  stream >> ob.cost;
  return stream;
}
int main()
{
  inventory ob("hammer", 4, 12.55);
  cout << ob;
  cin >> ob;
  cout << ob;
  return 0;
}


Override and input output stream operator

   
#include <iostream>
#include <string.h>
using namespace std;
class Employee 
{
 public:
   Employee(void) {};
   Employee(char *name, char sex, int age, char *phone) 
   {
     strcpy(Employee::name, name);
     Employee::sex = sex;
     Employee::age = age;
     strcpy(Employee::phone, phone); 
   };
     friend ostream &operator<<(ostream &cout, Employee emp);
     friend istream &operator>>(istream &stream, Employee &emp);
 private:
   char name[256];
   char phone[64];
   int age;
   char sex;
};
ostream &operator<<(ostream &cout, Employee emp)
 {
   cout << "Name: " << emp.name << "\tSex: " << emp.sex;
   cout << "\tAge: " << emp.age << "\tPhone: " << emp.phone << endl;
   return cout;
 }

istream &operator>>(istream &stream, Employee &emp)
{
   cout << "Enter Name: ";
   stream >> emp.name;
   cout << "Enter Sex: ";
   stream >> emp.sex;
   cout << "Enter Age: ";
   stream >> emp.age;
   cout << "Enter Phone: ";
   stream >> emp.phone;
   return stream;
}
int main(void)
{
   Employee worker;
   cin >> worker;
   cout << worker ;
}