Friendly operator+ : Plus « Overload « C++






Friendly operator+

  
#include <iostream>
#include <string.h>
using namespace std;
class String{
   public:
      String();
      String(const char *const);
      String(const String &);
      ~String();

      char & operator[](int offset);
      char operator[](int offset) const;
      String operator+(const String&);
      friend String operator+(const String&, const String&);
      void operator+=(const String&);
      String & operator= (const String &);

      int GetLen()const { return itsLen; }
      const char * GetString() const { return itsString; }

   private:
      String (int);
      char * itsString;
      unsigned short itsLen;
};

String::String()
{
   itsString = new char[1];
   itsString[0] = '\0';
   itsLen=0;
}

String::String(int len)
{
   itsString = new char[len+1];
   for (int i = 0; i<=len; i++)
      itsString[i] = '\0';
   itsLen=len;
}

String::String(const char * const cString)
{
   itsLen = strlen(cString);
   itsString = new char[itsLen+1];
   for (int i = 0; i<itsLen; i++)
      itsString[i] = cString[i];
   itsString[itsLen]='\0';
}

String::String (const String & rhs)
{
   itsLen=rhs.GetLen();
   itsString = new char[itsLen+1];
   for (int i = 0; i<itsLen;i++)
      itsString[i] = rhs[i];
   itsString[itsLen] = '\0';
}

String::~String ()
{
   delete [] itsString;
   itsLen = 0;
}

String& String::operator=(const String & rhs)
{
   if (this == &rhs)
      return *this;
   delete [] itsString;
   itsLen=rhs.GetLen();
   itsString = new char[itsLen+1];
   for (int i = 0; i<itsLen;i++)
      itsString[i] = rhs[i];
    itsString[itsLen] = '\0';
    return *this;
}

char & String::operator[](int offset)
{
   if (offset > itsLen)
      return itsString[itsLen-1];
   else
      return itsString[offset];
}

char String::operator[](int offset) const
{
   if (offset > itsLen)
      return itsString[itsLen-1];
   else
      return itsString[offset];
}
String String::operator+(const String& rhs)
{
   int  totalLen = itsLen + rhs.GetLen();
   String temp(totalLen);
   int i, j;
   for (i = 0; i<itsLen; i++)
      temp[i] = itsString[i];
   for (j = 0, i = itsLen; j<rhs.GetLen(); j++, i++)
      temp[i] = rhs[j];
   temp[totalLen]='\0';
   return temp;
}

String operator+(const String& lhs, const String& rhs)
{
   int  totalLen = lhs.GetLen() + rhs.GetLen();
   String temp(totalLen);
   int i, j;
   for (i = 0; i<lhs.GetLen(); i++)
      temp[i] = lhs[i];
   for (j = 0, i = lhs.GetLen(); j<rhs.GetLen(); j++, i++)
      temp[i] = rhs[j];
   temp[totalLen]='\0';
   return temp;
}

int main()
{
   String s1("String One ");
   String s2("String Two ");
   char *c1 = { "C-String One " } ;
   String s3;
   String s4;
   String s5;

   cout << "s1: " << s1.GetString() << endl;
   cout << "s2: " << s2.GetString() << endl;
   cout << "c1: " << c1 << endl;
   s3 = s1 + s2;
   cout << "s3: " << s3.GetString() << endl;
   s4 = s1 + c1;
   cout << "s4: " << s4.GetString() << endl;
   s5 = c1 + s2;
   cout << "s5: " << s5.GetString() << endl;
 return 0;
}
  
    
  








Related examples in the same category

1.Overload the + relative to MyClass.Overload the + relative to MyClass.
2.Overload + for 'ob + int' as well as 'ob + ob'.Overload + for 'ob + int' as well as 'ob + ob'.
3.String class with custom +/- operator
4.additional meanings for the + and = operations
5.overload the "+" operator so that several angles, in the format degrees minutes seconds, can be added directly.
6.overloaded '+' operator adds two Distances
7.overloaded '+' operator concatenates strings
8.+ is overloaded for three_d + three_d and for three_d + int.
9.Define operator +(plus) and cast to double operator
10.Overloading the + (or any other binary operator) using a friend allows a built-in type to occur on the left or right side of the operator.
11.friend overloaded + operator