18. Create a class string which contain a character pointer.write a c++ program to overload following operater : 1. < to compair two string (using new operater) 2. != to check equality two string.


#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *cp;
int len;
public:
void get(char *s)
{
len=strlen(s);
cp=new char[len+1];
}
void operator>(string &str)
{
int m=strlen(cp);
int n=strlen(str.cp);
if(m>n)
{
cout<<“\nString Second is Greater: \n”<< m;
}
else
{
cout<<“\nString First is Greater: \n”<<n;
}
}
void operator!=( string &str1)
{
int m=strlen(cp);
int n=strlen(str1.cp);
if(m!=n)
{
cout<<“\nString is Not equal: \n”<<m;
}
else
{
cout<<“\nString is equal: \n”<<n;
}
}
};
void main()
{
string s1,s2;
char ch[20];
clrscr();
cout<<“\nEnter the String 1st: \n”;
cin>>ch;
s1.get(ch);
cout<<“\nEnter the String 2nd: \n”;
cin>>ch;
s2.get(ch);
s1>s2;
s1!=s2;
getch();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s