Академический Документы
Профессиональный Документы
Культура Документы
In programming, using the same name for two or more functions. The compiler determines
which function to use based on the type of function, arguments passed to it and type of values
returned.
*C++ program to concatinate two strings using operator overloading and friend
function*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *p;
int l;
public:
string()
{
p=0;
l=0;
}
string(const char *s);
string(const string &S);
friend string operator+(const string &s,const string &t);
friend int operator<=(const string &s,const string &t);
friend void show(const string s);
};
string::string(const char *s)
{
l=strlen(s);
p=new char[l+1];
strcpy(p,s);
}
string::string(const string &S)
{
l=S.l;
p=new char[l+1];
strcpy(p,S.p);
}
string operator+(const string &s,const string &t)
{
string temp;
temp.l=s.l+t.l;
temp.p=new char[temp.l+1];
strcpy(temp.p,s.p);
strcat(temp.p,t.p);
return(temp);
}
int operator<=(const string &s,const string &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if(m<=n)
return(1);
else
return(0);
}
void show(const string s)
{
cout<<s.p;
}
void main()
{
clrscr();
string s1="New",s2="York",s3="Delhi",t1=s1,t2=s2,t3=s1+s3;
show(t1);cout<<endl;
show(t2);cout<<endl;
show(t3);cout<<endl;
if(t1<=t3)
{
show(t1);
cout<<" smaller than ";
show(t3);
}
else
{
show(t3);
cout<<" smaller than ";
show(t1);
}
getch();
}
OUTPUT:
New
York
NewDelhi
New smaller than NewDelhi