String concatenation using operator overloading
-
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;
} -
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;
} -
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;
}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