Make overloading functions for combining string class object

Make overloading functions for combining string class objects. For string object combination, you need two target operators string 1 + string target operators are two strings Implement \"operator +\" with following actions String 1 = string 2 + string 3; string 2 = \'happy\' string 3 = \'birthday\' string 1 = string 2 + string 3; string 1 = \'happy birthday\'; Implement \"operator +\" that allows multiple operation. String 1 = string 2 + string 3 + string 4, string 2 = \'happy\'; string3 = \'birthday\' string 4 = \'to you\'; string 1 = string 2 + string 3 + string 4; string 2 = \'happy birthday to you\';

Solution

#include\"iostream\"
using namespace std;
#include<string.h>
class string1
{
        public:
        char setofchar[50];                                     // String with maximum size 50
        string1()                                               // Zero argument Constructor
        {
                strcpy(setofchar,\"\");
        }
        string1(char a[])                                       // One argument Constructor
        {
                strcpy(setofchar,a);                          
        }
        string1 operator+(string1 second)                       // overload function of operator +
        {
                string1 temp;
                strcpy(temp.setofchar, setofchar);              // for copy the value to temporary variable
                strcat(temp.setofchar, \" \");                    // for add space in between two string
                strcat(temp.setofchar, second.setofchar);       // for adding second string with first string
                return temp;
        }
};
int main()
{
        string1 first(\"CHEGG\"), second(\"COMPUTER\"), sum;
        sum = first + second;
        cout<<sum.setofchar;                                    // for printing the output.
        return 0;
}

 Make overloading functions for combining string class objects. For string object combination, you need two target operators string 1 + string target operators

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site