Strminus.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 1k
Development Platform:

C++ Builder

  1. /* Another way to overload the - operator */
  2. #include <iostream.h>
  3. #include <iomanip.h>
  4. #include <string.h>
  5. class String {
  6.   public: 
  7.     char *operator +(char *append_str)
  8.       { return(strcat(buffer, append_str)); };
  9.    
  10.     char *operator -(char letter);
  11.     String(char *string) 
  12.       { strcpy(buffer, string); 
  13.         length = strlen(buffer); }
  14.     void show_string(void) { cout << buffer; };
  15.   private:
  16.     char buffer[256];
  17.     int length;
  18. };
  19. char *String::operator -(char letter)
  20.  { 
  21.    char target[256];
  22.    int i, j;
  23.    for (i = 0, j = 0; buffer[j]; j++) 
  24.      if (buffer[j] != letter)
  25.        target[i++] = buffer[j];
  26.    target[i] = NULL;
  27.    
  28.    for (i = 0, j = 0; (buffer[j] = target[i]); i++, j++)
  29.      ; 
  30.    return(buffer);
  31.  }
  32. void main(void)
  33.  {
  34.    String title("Jamsa's C/C++ ");
  35.    title = title + "Programmer's Biblen";
  36.    title.show_string();
  37.    title = title - '0';
  38.    title.show_string();
  39.  }