Program of operator overloading

Implement a class containing following functions.

1) Overload + operator to concatenate a string.
2) Overload = operator to carry out string copy.
3) Overload <= operator carry out the comparison of string.
4) Function tolower to convert uppercase letter to lowercase letter.
5) Function toupper to convert lowercase letter to uppercase letter.


#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>
class string
{
char s[30];
public:
void getdata()
{
gets(s);
}

void operator +(string ob)
{
strcat(ob.s,s);
cout<<"String after concatenation:"<<ob.s<<"\n";
}
void operator =(string ob1)
{
strcpy(s,ob1.s);
cout<<"String after copy of string:"<<s<<"\n";
}
void operator <=(string ob2)
{
int len1,len2;
len1=strlen(s);
len2=strlen(ob2.s);
cout<<"lenghth of string("<<s<<"):"<<len1;
cout<<"\nLenghth of string("<<ob2.s<<"):"<<len2;
if(len1>len2)
{
cout<<"\nlargest string is:"<<s;
}
else
{
cout<<"\nlargest string is:"<<ob2.s;
}
}
void Tolower()
{
int i;
for(i=0;s[i]!='\0';i++)
{
s[i]=tolower(s[i]);
}
cout<<"\nIn lower case:";
cout<<s;
}
void Toupper()
{
int j;
for(j=0;s[j]!=NULL;j++)
{
s[j]=toupper(s[j]);
}
cout<<"\nIn upper case:";
cout<<s;
}
};
void main()
{
clrscr();
string s1,ob5,ob6,ob7;
cout<<"First string:";
s1.getdata();
cout<<"second string:";
ob5.getdata();
ob5+s1;
ob6=s1;
ob5<=s1;
s1.Tolower();
s1.Toupper();
getch();
}

Share this

Related Posts

Previous
Next Post »

Powered by Blogger.

Popular Posts