Friday, January 24, 2014

C++ Program to Concatenate Two String Using the Concept of Operator Overloading

with 0 Comment
OPERATOR OVERLOADING

AIM:
  • To write a C++ program to concatenate two string using the concept of Operator Overloading.
ALGORITHM:
1. Start the program
2. Include suitable header file
3. Create a class with necessary variables and function
4. Get the value as two variables as concatenating
5. Overload an operator to concatenate two string
6. Execute the program
7. Stop the program

CODING:
#include<iostream.h>
#include<string.h>
#include<conio.h>
const int bufsize=50;
class string
{
private:
char str[bufsize];
public:
string()
{
strcpy(str," ");
}
string(char*mystr)
{
strcpy(str,mystr);
}
void echo()
{
cout<<str;
}
string operator+(string s)
{
string temp=str;
strcat(temp.str,s.str);
return temp;
}
};
void main()
{
clrscr();
string str1=" stud";
string str2=" ent";
string str3;
cout<<"\n before str3=str1=str2;.";
cout<<"\n str1=";
str1.echo();
cout<<"\nstr2=";
str2.echo();
cout<<"\n str3=";
str3.echo();
str3=str1+str2;
cout<<"\n\n after str3=str1+str2:..";
cout<<"\n str1=";
str1.echo();
cout<<"\n str2=";
str2.echo();
cout<<"\n str3=";
str3.echo();
getch();
}

OUTPUT:

Before str3=str1=str2;
str1=stud
str2=ent
str3=
After
str3=str1+str2:
str1=stud
str2=ent
str3=student

RESULT:
  • Thus to write a c++ program to concatenate two string using the concept of operator overloading was successfully completed.

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive