This is a sample program on operator overloading. Using operator overloading, here the user enters a string and compare it with a predefined string.
#include <iostream.h>
#include <conio.h>
#include <string.h>
class string
{
char st [30];
public:
string ( )
{
strcpy (st, " ");
}
string (char s [ ] )
{
strcpy (st, s);
}
void display ( )
{
cout << st;
}
int operator >= (string);
void read ( )
{
cin >> st;
}
};
int string :: operator >= (string ss2)
{
if (strcmp (st, ss2.st) >= 0)
return (1);
else
return (0);
}
int main ( )
{
clrscr ( );
string s1 ="Hello World";
string s2;
cout<<"Enter String: ";
s2.read ( );
if ( s1 >= s2)
cout<<"Alphabetically The String You Entered Is Greater. ";
else
cout<<"Alphabetically The String You Entered Is Lesser. ";
getch ( );
return 0;
}
OUTPUT:
Enter String: Good Morning
Alphabetically The String You Entered Is Greater.
Comments
Post a Comment
Please share your opinions and suggestions or your experience in the comments section. This way we can all help each other...
Experienced guys can share their resumes at admin@interview-made-easy.com