C++/Overload/Minus

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

Demo: Overload the +, -, and = relative to 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; 
  }
  void getXY(int &i, int &j) { 
     i=x; 
     j=y; 
  }
  MyClass operator+(MyClass object2);
  MyClass operator-(MyClass object2);
  MyClass operator=(MyClass object2);
};
MyClass MyClass::operator+(MyClass object2){
  MyClass temp;
  temp.x = x + object2.x;
  temp.y = y + object2.y;
  return temp;
}
MyClass MyClass::operator-(MyClass object2){
  MyClass temp;
  temp.x = x - object2.x;
  temp.y = y - object2.y;
  return temp;
}
MyClass MyClass::operator=(MyClass object2)
{
  x = object2.x;
  y = object2.y;
  return *this; // return the object that is assigned
}
int main()
{
  MyClass object1(10, 10), object2(5, 3), object3;
  int x, y;
  object3 = object1 + object2;          // add two objects - this calls operator+()
  object3.getXY(x, y);
  cout << "(object1+object2) X: " << x << ", Y: " << y << endl;
  object3 = object1 - object2;          // subtract two objects
  object3.getXY(x, y);
  cout << "(object1-object2) X: " << x << ", Y: " << y << endl;
  object3 = object1;               // assign an object
  object3.getXY(x, y);
  cout << "(object3=object1) X: " << x << ", Y: " << y << endl;
  return 0;
}


Overload - (subtraction) so that it removes the first occurrence of the substring on the right from the string on the left and returns the result.

  
#include <iostream>
#include <string>
using namespace std;
string operator-(const string &left, const string &right);
string operator-=(string &left, const string &right);
int main()
{
  string str("This is a test.");
  string res_str;
  cout << "Contents of str: " << str << "\n\n";
  res_str =  str - "is";
  cout << res_str << "\n\n";
  res_str -= "is";
  cout << res_str << "\n\n";
  cout << str;
  return 0;
}

string operator-(const string &left, const string &right) {
  string::size_type i;
  string result(left);
  i = result.find(right);
  if(i != string::npos)
    result.erase(i, right.size());
  return result;
}

string operator-=(string &left, const string &right) {
  string::size_type i;
  i = left.find(right);
  if(i != string::npos)
    left.erase(i, right.size());
  return left;
}


overload the - operator

   
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
class String {
  public: 
    char *operator +(char *append_str)
    { return(strcat(buffer, append_str)); };
   
    char *operator -(char letter);
    String(char *string) 
      { strcpy(buffer, string); 
        length = strlen(buffer); }
    void show_string(void) { cout << buffer; };
  private:
    char buffer[256];
    int length;
};
char *String::operator -(char letter)
{ 
   char target[256];
   int i, j;
   for (i = 0, j = 0; buffer[j]; j++) 
     if (buffer[j] != letter)
       target[i++] = buffer[j];
   target[i] = NULL;
   
   for (i = 0, j = 0; (buffer[j] = target[i]); i++, j++)
     ; 
   return(buffer);
}
int main(void)
{
   String title("C");
   title = title + "B\n";
   title.show_string();
   title = title - "0";
   title.show_string();
}


Overload the - relative to MyClass class 1.

  
#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; 
  }
  void getXY(int &i, int &j) { 
     i=x; 
     j=y; 
  }
  MyClass operator-(MyClass object2); // binary minus
  MyClass operator-();              // unary minus
};
MyClass MyClass::operator-(MyClass object2)
{
  MyClass temp;
  temp.x = x - object2.x;
  temp.y = y - object2.y;
  return temp;
}
// Overload unary - for MyClass class.
MyClass MyClass::operator-()
{
  x = -x;
  y = -y;
  return *this;
}
int main()
{
  MyClass object1(10, 10), object2(5, 7);
  int x, y;
  object1 = object1 - object2;                // subtraction
  object1.getXY(x, y);
  cout << "(object1-object2) X: " << x << ", Y: " << y << endl;
  object1 = -object1;                    // negation
  object1.getXY(x, y);
  cout << "(-object1) X: " << x << ", Y: " << y << endl;
  return 0;
}