You are here:

C/operator overloading

Advertisement


Question
please check my program of c++. i am trying to overload += operator for 2strings

#include <iostream>
#include <cstring>
using namespace std;

class strng
{
 char s[30];
   public:
          strng()
          {
                  strcpy(s,"");
                  }
    string getstring()
         {
            cout<<"enter the sting:";
            cin>>s;
          return s;  
                       }
    void displaystring()
    {
          cout <<"the string is:"<<s<<endl;
          }

                  
                         
                       const strng &operator+= (const strng &t);
                          };
                          const strng &strng ::operator+=(const strng &t)
                            {
                                   strng temp;
                                   strcpy (temp.s,"");
                                   
                                   strcat(temp.s,s);
                                   strcat(temp.s,t.s);
                                   s=temp;
                                   return s;
                                   }
                                   
                                   
                                   
          main()
         {
                    strng s1;
                    strng s2;
                    s1.getstring();
                    s2.getstring();
                     s1+=s2;
                    s1.displaystring();
                            

}

Answer
Hello Sidra.

Your operator+= method needs to take the characters in the parameter 't', and add them to the 's' array in the object that the operator is being called on. You don't need any temp object. Here is the idea.

const strng &strng ::operator+=(const strng &t)
{
   //This function adds the stuff in t to the stuff in this strng instance
   strcat(s, t.s); // This is all you need to do
   return *this; //you need to return the object, not the data in the object
}

Now you are missing one very important thing and that is checking that there is enough room in the s array to hold the string. You need that checking in the operator+=, and in the getstring methods. If you do not do error checking, it is easy for the the s array to overflow. If the user types in more than 30 characters, cin will just write past the end of s and your program will probably crash.

One easy way to do the input is to use a real c++ string object and the readline function.
   void getstring()
   {
       cout<<"enter the sting:";
       string temp;
       getline(cin, temp);
       strncpy(s, temp.c_str(), sizeof(s));
       s[sizeof(s)-1] = 0;
   }

Notice that getstring does not return anything. It operates on the data of the object that it's called on.
The getline will resize temp so that there is no buffer overflow.
Then you can copy characters from the temp string into the s array making sure that you don't write past the end of the s array. Notice also, that I refer to sizeof(s), instead of putting in 30. If you change the size of s from 30 to 40 you only have to make the change in one place. Finally, s[sizeof(s)-1] makes sure that the s array is null terminated since strncpy will not automatically do that.

As an alternative, if the user puts in more than 30 characters into temp, you can display an error, and make the user do it again.

I hope that helps you.

Best regards
Zlatko

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.