Make overloading functions for combining string class objects. For string object
ID: 3934723 • Letter: M
Question
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';Explanation / Answer
#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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.