+ 8

Explain Output please

Run this code multiple time you will get you will get different characters which are not supposed to be print . https://code.sololearn.com/crwwhuyG4wM1/?ref=app

14th Feb 2018, 11:59 AM
Tanay
Tanay - avatar
2 Answers
+ 5
still it is giving unnecessary character in output.
15th Feb 2018, 6:52 AM
Tanay
Tanay - avatar
+ 1
The + operator. has been wrongly overloaded, as you intend to use it as a binary operation (2 strings to one) but the copying part has some errors. I will just post the correct overloaded version. Edit : Use this instead: //overloading + operator str operator+ (str s) { str temp; // No need of an extra char*. temp.len=strlen(p)+strlen(s.p)+1; temp.p=new char[temp.len]; strcpy(temp.p,p); // Copy p to temp. strcat(temp.p,s.p); // Concat s.p to temp. return temp; }
15th Feb 2018, 4:13 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar