Thursday, 3 April 2014

String Comparison & Concatanation Using Operator Overloading..

#include<iostream.h>
#include<string.h>
Class Strtype
{
char str1[10];
Public:
          void Input();
          void Display();
Strtype operator+(Strtype);

int operator<(Strtype);
int operator>(Strtype);
int operator==(Strtype);
} ;

void main()
{
Strtype ob1,ob2,ob3;

ob1.Input();
ob2.Input();
ob1.Display();
ob2.Display();
ob3=ob1+ob2;
ob3.Display();

if(ob1<ob2)
{
cout<<"\n First String Is Less Than Second";
}
else if(ob1>ob2)
{
cout<<"\n First String Is Greater Than Second";
}
else if(ob1==ob2)
{
cout<<"\n Strings Are Equal";
}
getch();
} 

void Strtype::Input()
{
cout<<"\n Enter a string";
cin>>str1;
}

void Strtype::Display()
{
cout<<"|n The String Is :-";
cout<<"str1";
}

Strtype Strtype::operator+(Strtype ob2)
{
Strtype temp;
Strcpy(temp.str1,str1);
Strcat(temp.str1,ob2.str1);
return(temp);
}

int Strtype::operator<(Strtype ob2)
{
if(Strcmp(str1,ob2.str1)<0)
     { 
     return(1);
     }
} 

int Strtype::operator==(Strtype ob2)
{
  if(Strcmp(str1,ob2.str1)==0)
     {
      return(1);
      }
}

int Strtype::operator>(Strtype ob2)
{
 if ( Strcmp(str1,ob2.str1)>0)
   {
     return(1);
   }
}
 


No comments:

Post a Comment