Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 04-19-2005, 06:43 AM   #1 (permalink)
arnie6
Registered User
 
Join Date: Apr 2005
Posts: 1
arnie6 is on a distinguished road
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.
Code:
#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;                        
}

Last edited by redhead; 04-19-2005 at 07:33 AM. Reason: Use [ code] tags
arnie6 is offline   Reply With Quote
Old 04-19-2005, 09:52 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
I'll be back later. In the mean time, observe how I define a custom class String. See if anything can be learned from it. I think so.

Code:
#include <iostream>
#include <cstring>

using namespace std;

class String
{
private:
  friend int operator==(const String& lhs, const String& rhs);
  friend ostream& operator<<(ostream& os, String& s);
public:  
  String& operator=(const String&);
  String& operator+=(const String&);     

public:
  ~String ();
  String(const char* = "");
  String(const String&); 
private:
  char *m_pChars;
};

//Acts like a conversion operator: 'c-style char' to 'String'
String::String(const char *s) 
{
  // need to copy the given string. If s is zero, make a null string!
  if (s)
  {  
    int len = strlen(s) + 1;
    m_pChars = new char[len];
    strcpy(m_pChars, s); 
  }
  else
  {
    m_pChars = new char[1];
    *m_pChars = '\0';
  }
}

String::String(const String& s)
{
  m_pChars = new char[strlen(s.m_pChars) + 1];
  strcpy(m_pChars, s.m_pChars);
} 

String::~String() { delete[] m_pChars; }

String& String::operator=(const String& s) 
{
  if (this != &s)
  {  
    delete[] m_pChars; // first delete old
    int len = strlen(s.m_pChars) + 1;
    m_pChars = new char[len];
    strcpy(m_pChars, s.m_pChars);
  }  
  return *this;
} 

//MUST be defined outside class; So don't forget 
//to make it a friend in the class that uses it.
ostream& operator<<(ostream& os, String& s)
{
  os<<s.m_pChars;
  return os; //Always return inserter/extractor.
}

int operator==(const String& lhs, const String& rhs) 
{ 
  return (strcmp(lhs.m_pChars,rhs.m_pChars) == 0); 
}

String& String::operator+=(const String& s)
{
  char* buffer = new char[strlen(m_pChars) + strlen(s.m_pChars) + 1]; 
  strcpy(buffer, m_pChars);
  strcat(buffer, s.m_pChars); 
  delete[] m_pChars;
  m_pChars = buffer;
  return *this;
} 

String operator+(const String& lhs, const String& rhs)
{
  String buffer(lhs); // calls copy ctor 
  buffer += rhs; // calls our overloaded +=
  return buffer;  
}

//----------------------------------------------------------

int main()
{
  String A("Hi baby.");
  String B(" Wanna dance?");
  String C(A+B);
  C = A + B;
  
  cin.get();
  
  return 0;
}
__________________
Valmont is offline   Reply With Quote
Old 04-21-2005, 05:12 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Have I forgotten this thread?

Okidoky.
You won't get full code from me. That hard part you will do. But here is a nice visualisation:

Suppose the String is "Hello world.".
And the sub-string is " world".
The final result is "Hello.".

Let's see how it looks in memory:

Code:
[H][e][l][l][o][][w][o]r[l][d][.][\0] <==m_pChars, len = 12
[][w][o][r][l][d][\0] <== substring (to remove), sublen = 6
[][w][o][r][l][d][.] <== const char* firstoccurence = strstr(m_pChars, substr.m_pChars);
std::size_t pos =  first - m_pChars; // First occurance position.
char* buffer = new char[len - sublen + 1];
//some magic here...
//delete[] m_pChars;
//Let m_pChars point to buffer
See if you can combine this information into a working part.

And if your teacher told you to overload operator-= for a custom string, tell him he should come here for a few lessons. By me.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:46 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting