p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C++ Programming (http://p2p.wrox.com/forumdisplay.php?f=89)
-   -   subtraction operator overload (http://p2p.wrox.com/showthread.php?t=28306)

arnie6 April 19th, 2005 07:57 AM

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



All times are GMT -4. The time now is 06:48 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.