Search This Blog

Tuesday, 13 November 2012

Define a class String Using operator overloading do all operations on the string.

#include<iostream.h>
#include<conio.h>
#include<string.h>e
class String{
char *s;
public:
String(){}
String(char *t)
{
int |=strlen(t);
s=new char[|+1];
strcpy(s,t);
}
void getstring()
{
cout<<"Get String";
cin>>s;
}
void display()
{
cout<<"Given String="<<s<<"\n";
}
string operator+(String T)
{
String T1;
int |=strlen(T.s)+strlen(s);
T1.s=new char[|+1];
strcpy(T1.s,s);
strcpy(T1.s,T.s);
return T1;
}
void operator=(string T)
{
int |=strlen(T.s);
s=new cahr[|+1];
strcpy(s,T.s);
}
int operator>(string T)
{
if(strcmp(s,T.s)>0)
return 1;
else
return 0;
}
int operator<(string T)
{
if(strcmp(s,T.s)<0)
return 1;
else
return 0;
}
};
void main()
{
String s1("welcome"),s2("csc"),s3;
s3=s1+s2;
s3.display();
string s4;
s4=s3;
s4.display();
if(s1>s2)
coaut<<"First Object is greater than second";
else
cout<<"less than";
getch();
}

output:
welcome csc
welcome csc
First Object is greater than second


No comments:

Post a Comment