#include<iostream>
#include<cstring>
#include<conio.h>
using namespace std;
class String
{
char *value;
int len;
public:
String()
{
len=0;
value=0;
}
~String() {}
String(char *s)
{
len=strlen(s);
value=new char[len+1];
strcpy(value,s);
}
String(String & s)
{
len=s.len;
value=new char[len+1];
strcpy(value,s.value);
}
friend String operator+(String obj1, String obj2)
{
String obj3;
obj3.len=obj1.len+obj2.len;
obj3.value=new char [obj3.len+1];
strcpy(obj3.value,obj1.value);
strcat(obj3.value,obj2.value);
return obj3;
}
friend String operator=(String obj1, String obj2)
{
String obj3;
strcpy(obj3.value,obj1.value);
strcat(obj3.value,obj2.value);
return obj3;
}
void display()
{ cout<<value<<endl; }
};
int main()
{
String s1("Bodacious ");
String s2("AllienBrain");
String s3;
s3=s1+s2;
s3.display();
getch();
}
As I am already operated the operator + in my code but i also want to overload the operator=
to conactenate both the strings but this code shows no error when i overload the + operator but it shows the correct output i.e. Bodacious AllienBrain.
But when i overload the operator=
it throws error so anyone tell me whats wrong with me?