overload the - operator : Minus « Overload « C++






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();
}
  
    
    
  








Related examples in the same category

1.Overload the - relative to MyClass class 1. Overload the - relative to MyClass class 1.
2.Demo: Overload the +, -, and = relative to MyClass.Demo: Overload the +, -, and = relative to MyClass.
3.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.