Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 19th, 2005, 07:57 AM
Registered User
 
Join Date: Apr 2005
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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Invalid operator for data type. Operator equals di Pusstiu SQL Server 2000 2 August 10th, 2007 04:51 AM
Subtraction in an Access Query pankaj_daga Access 2 April 13th, 2007 06:49 AM
Unary operator overload and inheritance Jonax C++ Programming 8 February 27th, 2006 08:50 AM
Dates Subtraction akhamis SQL Language 1 December 2nd, 2004 04:38 PM
subtraction collie SQL Server 2000 3 November 26th, 2004 01:03 AM





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