Create a class "string" as user defined string type. Include constructors to create and initialize a string. Overload the operators + and <= to add and compare two strings.

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class string
{
      char str[100];
   public:
      string(){strcpy(str,"");}
      string(char a[100]){strcpy(str,a);}
      void showstr(){cout<<str;}
      string operator +(string);
      int operator <=(string);
};
string string::operator +(string str2)
{
   string str3;
   strcpy(str3.str, str);
   strcat(str3.str,str2.str);
   return(str3);
}
int string::operator <=(string str2)
{
   if (strcmp(str,str2.str) > 0)
      return(0);
   else
      return(1);
}
void main()
{
   char s1[100], s2[100];
   clrscr();
   cout<<"Enter the First String : ";
   gets(s1);
   cout<<"Enter the Second String : ";
   gets(s2);
   string so1(s1), so2(s2), so3;
   cout<<endl<<"First String = ";
   so1.showstr();
   cout<<endl<<"------------------------------------------";
   cout<<endl<<"Second String = ";
   so2.showstr();
   cout<<endl<<"------------------------------------------";
   so3=so1+so2;
   cout<<endl<<"The Concatenated String = ";
   so3.showstr();
   cout<<endl<<"------------------------------------------";
   if (so1<=so2)
      cout<<endl<<"String-1 <= String-2";
   else
      cout<<endl<<"String-1 > String-2";
   cout<<endl<<"==========================================";
   getch();
}

No comments:

Post a comment