String Manipulation using Operator Overloading


C++ allows us the facility of manipulate strings using the concept of operator overloading.
For example we can overload + operator to concate two strings. We can overload == operator to compare two strings.
Consider Following Example in which we overload + operator to concate two strings.

     

#include<iostream.h>
#include<string.h>
class string
{
char *name;
int length;
public:
string()
{
length=0;
name = new char[length+1];
}
string(char *n)
{
length=strlen(n);
name= new char [length+1];
strcpy(name,n);
}
void display()
{
cout<<"String:"<<name;
}
string operator+(string s)
{
string temp;
strcpy(temp.name,name);
strcat(temp.name,s.name);
return temp;
}
};
int main()
{
string s1("Hello");
string s2("Welcome");
string s3;
s1.display();
s2.display();
s3=s1+s2;
s3.display();
return 0;
}

Download Projects


Download Programs