/*write a c++ program to create a class which contain a character pointer. write a c++
program to overload following operator
1.< to compare two string (using new operator)
2.!= to check equality of two string
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
int i=0;
class mystring
{
char *str;
public:void accept()
{
i=i+1;
str=new char[20];
cout<<"\nenter string "<<i<<":-";
cin>>str;
}
void operator <(mystring);
void operator !=(mystring);
};
void mystring::operator <(mystring m2)
{
int i;
i=strcmp(str,m2.str);
if(i<0)
cout<<"\nfirst string is less than second";
else
cout<<"\nsecond string is less than first";
}
void mystring ::operator!=(mystring m2)
{
int i;
i=strcmp(str,m2.str);
if(i!=0)
cout<<"strings are not equal";
else
cout<<"strings are equal";
}
int main()
{
mystring m1,m2,m3;
clrscr();
cout<<"\n\t\tto compare two string\n\n";
m1.accept();
m2.accept();
m1<m2;
cout<<"\n\t\tto check equality of two string\n\n";
m1.accept();
m2.accept();
m1!=m2;
getch();
return 0;
}
/*
to compare two string
enter string 1:-peer
enter string 2:-asd
second string is less than first
to check equality of two string
enter string 3:-peer
enter string 4:-peer
strings are equal
*/
program to overload following operator
1.< to compare two string (using new operator)
2.!= to check equality of two string
*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
int i=0;
class mystring
{
char *str;
public:void accept()
{
i=i+1;
str=new char[20];
cout<<"\nenter string "<<i<<":-";
cin>>str;
}
void operator <(mystring);
void operator !=(mystring);
};
void mystring::operator <(mystring m2)
{
int i;
i=strcmp(str,m2.str);
if(i<0)
cout<<"\nfirst string is less than second";
else
cout<<"\nsecond string is less than first";
}
void mystring ::operator!=(mystring m2)
{
int i;
i=strcmp(str,m2.str);
if(i!=0)
cout<<"strings are not equal";
else
cout<<"strings are equal";
}
int main()
{
mystring m1,m2,m3;
clrscr();
cout<<"\n\t\tto compare two string\n\n";
m1.accept();
m2.accept();
m1<m2;
cout<<"\n\t\tto check equality of two string\n\n";
m1.accept();
m2.accept();
m1!=m2;
getch();
return 0;
}
/*
to compare two string
enter string 1:-peer
enter string 2:-asd
second string is less than first
to check equality of two string
enter string 3:-peer
enter string 4:-peer
strings are equal
*/
No comments:
Post a Comment