Create a class string with character array as a data member and write a program to add two strings with use of operator overloading concept.
CODING:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char s[50];
public:
void get_string()
{
cout<<"NAME :";
cin>>s;
}
void disp_string()
{
cout<<s;
}
string operator+(string a)
{
string n;
strcat(s," ");
strcat(s,a.s);
strcpy(n.s,s);
return n;
}
};
void main()
{
string s1,s2,s3,s4;
clrscr();
cout<<"FIRST ";s1.get_string();
cout<<"LAST ";s2.get_string();
s3=s1+s2;
cout<<"FULL NAME :";s3.disp_string();
getch();
}
CODING:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char s[50];
public:
void get_string()
{
cout<<"NAME :";
cin>>s;
}
void disp_string()
{
cout<<s;
}
string operator+(string a)
{
string n;
strcat(s," ");
strcat(s,a.s);
strcpy(n.s,s);
return n;
}
};
void main()
{
string s1,s2,s3,s4;
clrscr();
cout<<"FIRST ";s1.get_string();
cout<<"LAST ";s2.get_string();
s3=s1+s2;
cout<<"FULL NAME :";s3.disp_string();
getch();
}
Comments
Post a Comment