C++/copying

Advertisement


Question
QUESTION: sir how can i copy my value of s1 into anothe string s3 using operator overloading function in the following program


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

class strng
{
 char s[30];
   public:
         strng()
         {
         strcpy(s,"");
         }
         

         void getstring()
  {
      cout<<"enter the sting:";
      string temp;
      getline(cin, temp);
      strncpy(s, temp.c_str(), sizeof(s));
      s[sizeof(s)-1] = 0;
  }


         
         
    void displaystring()
    {
         cout <<"the string is:"<<s<<endl;
         }
         

         const strng &operator+= (const strng &t);
         };


         const strng &strng ::operator+=(const strng &t)
         {
         
         strcat(s,t.s);
         return *this;
         }
         
         
         
         main()
         {
         strng s1;
         strng s2;
         strng s3;
s1.getstring();
         s2.getstring();
         s1+=s2;
         s1.displaystring();
         

}

ANSWER: Hello Sidra

You can use an operator= to do assignment from one string to another.

const strng &strng::operator= (const strng &rhs)
{
   if (this != &rhs)
   {
       strcpy(s, rhs.s);
   }
   return *this;
}

All operator= functions should have a comparison of "this" against the right-hand-side (rhs) to prevent work being done if the programmer wrote something like s1 = s1;

The operator returns a reference to "this" so that the user chain many assignments together, like this s1=s2=s3;

---------- FOLLOW-UP ----------

QUESTION: chk this code now
#include <iostream>
#include <cstring>
using namespace std;

class strng
{
 char s[30];
   public:
         strng()
         {
         strcpy(s,"");
         }
         

         void getstring()
  {
      cout<<"enter the sting:";
      string temp;
      getline(cin, temp);
      strncpy(s, temp.c_str(), sizeof(s));
      s[sizeof(s)-1] = 0;
  }


         
         
    void displaystring()
    {
         cout <<"the string is:"<<s<<endl;
         }
         

         const strng &operator+= (const strng &t);
         const strng &operator=(const strng &rhs);
         };


         const strng &strng ::operator+=(const strng &t)
         {
         
         strcat(s,t.s);
         return *this;
         }
         const strng &strng::operator= (const strng &rhs)
{
  if (this != &rhs)
  {
      strcpy(s, rhs.s);
  }
  return *this;
}

         
         
         
         
         main()
         {
         strng s1;
         strng s2;
         strng s3;
         s1.getstring();
         s2.getstring();
         s1+=s2;
         s1.displaystring();
         s3=s1;
         s3.displaystring();      
         

}

ANSWER: That looks good to me.

In my system, I had to have:
#include <string>

and I had to have main return an int. Your compiler may be different. If you are using Turbo C++, it will be using an old version of the C++ language so it may not require that main return an int. The latest C++ standard does require it.

---------- FOLLOW-UP ----------

QUESTION: can u tell me any other way through which i can copy s1 into s3 in the same += operator overloading function

Answer
If you want to implement operator+= without strcpy, you have to copy the characters to the end of this->s by individual characters.

You need to find the end of this->s, copy each character from t.s to the end of this->s, then add the null terminator to this->s.

It would be something like this in operator+=

int end = strlen(s); // end is the index past the last character in s
char* src = t.s; // src points to  the start of t.s
// do while there is a character pointed at by src
// and while the end index is in valid bounds.
while(*src && end < (sizeof(s)-1))
{
    s[end++] = *src;
    ++src;
}
s[end]  = 0;

I have not checked this with a compiler, but I think it is correct. I don't have access to a compiler today.

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.