View Single Post
  #1 (permalink)  
Old April 19th, 2005, 07:57 AM
arnie6 arnie6 is offline
Registered User
 
Join Date: Apr 2005
Location: , , United Kingdom.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default subtraction operator overload

Hi,
I'm a beginner.
Could anybody help me out with this.
How to make a class strin subtraction operator overload, that if it finds "Bill", then deletes it from the text string.
Thank you in advance.

#include <iostream.h>
#include <string.h>

class strin {
   char *place;
   int length;
public:
   strin (char *text);
   strin ();
   strin (strin &kit);
   ~strin () { delete[] place; }
   int getlen () { return length; }
   strin & operator + (strin &arg);

   strin & operator = (const strin &arg);

   void show() { cout << place << endl; }
};

main() {
   strin a ("The quick pretty brown cow jumps over Bill"),
   b ("Bill"), c;
   a.show();
   c = a + b;
   c.show();
   a.show();
   strin d(b);
   c.show();
}

strin :: strin (char *text) {
   length = strlen (text);
   place = new char[length+1];
   strcpy(place, text);
}

strin :: strin() {
   length = 0;
   place = new char[1];
   *place = '\0';
}

strin :: strin (strin &kit) {
   length = kit.length;
   place = new char[length+1];
   strcpy(place, kit.place);
}

strin & strin :: operator + (strin &arg) {
   strin *temp = new strin;
   temp -> length = length + arg.length;
   temp -> place = new char[temp->length+1];
   strcpy (temp->place, place);
   strcat (temp->place, arg.place);
   return *temp;
}

strin & strin :: operator = (const strin &arg) {
   if (this != &arg) {
      delete [] place;
      length = arg.length;
      place = new char[length+1];
      strcpy (place, arg.place);
   }
   return *this;
}

Reply With Quote