Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. String concatenation using operator overloading

String concatenation using operator overloading

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestion
3 Posts 3 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kinderu
    wrote on last edited by
    #1

    I have the following program and I am trying to do the conactenation of two strings using overloading operator "+", but I am getting error in function "String operator+ (String box)" and line "box4 = box1 + box2". Why doesn't show my string concatenated ?

    #include #include using namespace std;

    class String
    {
    public:
    char *sir;
    String()
    {
    cout << "\n String() default called." << endl;
    }
    String(char *sir)
    {
    cout << "\n String() parameter called." << endl;
    if (this->sir != NULL)
    delete this->sir;
    this->sir = new char[strlen(sir)+1];
    strcpy(this->sir, sir);
    }
    String(String &box)
    {
    cout << "\n String() copy constructor called." << endl;
    this->sir = new char[strlen(box.sir)+1];
    strcpy(this->sir, box.sir);
    }
    void setString(char *sir)
    {
    cout << "\n setString() called." << endl;
    this->sir = sir;
    }
    char *getString()
    {
    return sir;
    }
    String operator=(String box)
    {
    cout << "\n String() Assigment operator called." << endl;
    String temp;
    strcpy(temp.sir, sir);
    strcat(temp.sir, box.sir);
    return temp;
    }
    String operator+ (String box)
    {
    String temp;
    strcpy (temp.sir, sir);
    strcat (temp.sir, box.sir);
    return temp;
    }
    };

    int main()
    {
    String box1;
    box1.setString("Geeksforgeeks");
    cout << "\n Box1::sir: " << box1.getString() << endl;
    String box2;
    box2.setString("GeeksQuiz");
    cout << "\n Box2::sir: " << box2.getString() << endl;
    box1 = box2;
    cout << "\n Box1::sir: " << box1.getString() << endl;
    String box3 = box2;
    cout << "\n Box3::sir: " << box3.getString() << endl;
    String box4;
    box4 = box1 + box2;
    cout << "\n Box4::sir: " << box4.getString() << endl;
    return 0;
    }

    L K 2 Replies Last reply
    0
    • K kinderu

      I have the following program and I am trying to do the conactenation of two strings using overloading operator "+", but I am getting error in function "String operator+ (String box)" and line "box4 = box1 + box2". Why doesn't show my string concatenated ?

      #include #include using namespace std;

      class String
      {
      public:
      char *sir;
      String()
      {
      cout << "\n String() default called." << endl;
      }
      String(char *sir)
      {
      cout << "\n String() parameter called." << endl;
      if (this->sir != NULL)
      delete this->sir;
      this->sir = new char[strlen(sir)+1];
      strcpy(this->sir, sir);
      }
      String(String &box)
      {
      cout << "\n String() copy constructor called." << endl;
      this->sir = new char[strlen(box.sir)+1];
      strcpy(this->sir, box.sir);
      }
      void setString(char *sir)
      {
      cout << "\n setString() called." << endl;
      this->sir = sir;
      }
      char *getString()
      {
      return sir;
      }
      String operator=(String box)
      {
      cout << "\n String() Assigment operator called." << endl;
      String temp;
      strcpy(temp.sir, sir);
      strcat(temp.sir, box.sir);
      return temp;
      }
      String operator+ (String box)
      {
      String temp;
      strcpy (temp.sir, sir);
      strcat (temp.sir, box.sir);
      return temp;
      }
      };

      int main()
      {
      String box1;
      box1.setString("Geeksforgeeks");
      cout << "\n Box1::sir: " << box1.getString() << endl;
      String box2;
      box2.setString("GeeksQuiz");
      cout << "\n Box2::sir: " << box2.getString() << endl;
      box1 = box2;
      cout << "\n Box1::sir: " << box1.getString() << endl;
      String box3 = box2;
      cout << "\n Box3::sir: " << box3.getString() << endl;
      String box4;
      box4 = box1 + box2;
      cout << "\n Box4::sir: " << box4.getString() << endl;
      return 0;
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What error, and where does it occur?

      1 Reply Last reply
      0
      • K kinderu

        I have the following program and I am trying to do the conactenation of two strings using overloading operator "+", but I am getting error in function "String operator+ (String box)" and line "box4 = box1 + box2". Why doesn't show my string concatenated ?

        #include #include using namespace std;

        class String
        {
        public:
        char *sir;
        String()
        {
        cout << "\n String() default called." << endl;
        }
        String(char *sir)
        {
        cout << "\n String() parameter called." << endl;
        if (this->sir != NULL)
        delete this->sir;
        this->sir = new char[strlen(sir)+1];
        strcpy(this->sir, sir);
        }
        String(String &box)
        {
        cout << "\n String() copy constructor called." << endl;
        this->sir = new char[strlen(box.sir)+1];
        strcpy(this->sir, box.sir);
        }
        void setString(char *sir)
        {
        cout << "\n setString() called." << endl;
        this->sir = sir;
        }
        char *getString()
        {
        return sir;
        }
        String operator=(String box)
        {
        cout << "\n String() Assigment operator called." << endl;
        String temp;
        strcpy(temp.sir, sir);
        strcat(temp.sir, box.sir);
        return temp;
        }
        String operator+ (String box)
        {
        String temp;
        strcpy (temp.sir, sir);
        strcat (temp.sir, box.sir);
        return temp;
        }
        };

        int main()
        {
        String box1;
        box1.setString("Geeksforgeeks");
        cout << "\n Box1::sir: " << box1.getString() << endl;
        String box2;
        box2.setString("GeeksQuiz");
        cout << "\n Box2::sir: " << box2.getString() << endl;
        box1 = box2;
        cout << "\n Box1::sir: " << box1.getString() << endl;
        String box3 = box2;
        cout << "\n Box3::sir: " << box3.getString() << endl;
        String box4;
        box4 = box1 + box2;
        cout << "\n Box4::sir: " << box4.getString() << endl;
        return 0;
        }

        K Offline
        K Offline
        KarstenK
        wrote on last edited by
        #3

        You must allocate some memory (and delete it after your done) for your sir member.

        String operator+ (String box)
        {
        String temp;
        temp.sir = new char[strlen(box.sir)]:///memory for the string
        strcpy (temp.sir, sir);
        strcat (temp.sir, box.sir);
        return temp;
        }

        I am a bit confused that your code isnt crashing. X|

        Press F1 for help or google it. Greetings from Germany

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups