This is a sample C++ program on Operator Overloading. Using Operator Overloading, here we will concatenate two strings.
#include <iostream.h>
#include <conio.h>
#include <string.h>
#define SIZE 40
class string
{
char st [SIZE];
public:
string ( )
{
strcpy (st, " ");
}
string (char s[ ] )
{
strcpy (st, s);
}
void show ( )
{
cout << st;
}
string operator+ (string);
};
string string :: operator+ (string ss2)
{
string temp;
strcpy (temp.st, st);
strcat (temp.st, ss2.st);
return (temp);
}
int main ( )
{
clrscr ( );
string s1 = "Good";
string s2 = "Day :)";
string s3;
cout << "The Strings Before Concatenation Is \n ";
cout << "String1 = ";
s1.show ( );
cout << " \n String2 = ";
s2.show ( );
s3 = s1 + s2;
cout << "\nThe Strings After Concatenation Is \n ";
cout << '\n';
s3.show ( );
getch ( );
return 0;
}
OUTPUT:
The Strings Before Concatenation Is
String1 = Good
String2 = Day :)
The Strings After Concatenation Is
GoodDay :)
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com