Overload Less Than Operator To Find Which Of The Two String Object Is Greater

overload < (less than) operator to find which of the two string object is greater and show the greater string object



Write an OOP program to overload < (less than) operator to find which of the two string object is greater and show the greater string object.

SOURCE CODE ::
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class string
{
     char s1[20];
    public:
     void get()
     {
    cout<<"\n\n Enter any string : ";
    gets(s1);
     }
     void show()
     {
    cout<<"\n\n Greater String is : ";
    puts(s1);
    cout<<"\n\n";
     }
    int operator <(string x)
     {
       int l1=strlen(s1);
       int l2=strlen(x.s1);
       if(l1>l2)
       return(1);
       else
       return(0);
     }
};
void main()
{
  string obj,obj1;
  clrscr();
  obj.get();
  obj1.get();
  if(obj<obj1)
  {
    obj.show();
  }
  else
  {
    obj1.show();
  }
  getch();
}

OUTPUT ::
 Enter any string : FOURTH
 Enter any string : SEMESTER
 Greater String is : SEMESTER


Labels: