Thursday, 7 March 2019

CPP-EX2


WAP to to create class and using operator overloading >,!=,+

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
class ms
{
char *s;
public:
ms();
void dis();
void operator>(ms);
void operator!=(ms);
ms operator+(ms);
};
ms::ms()
{
 int n;
cout<<"\nEnter the len string";
 cin>>n;
s=new char[n+1];
cout<<"\nEnter string";
for(int i=0;i<n;i++)
cin>>(s+i);
*(s+i)='\0';
}
void ms::dis()
{
cout<<s;
}
void ms::operator>(ms str)
{
int l1,l2;
l1=strlen(s);
l2=strlen(str.s);
if(l1>l2)
{
cout<<"\nfirst string is greater";
}
else if(l2>l1)
{
cout<<"\nSecond string is greater";
}
else
{
cout<<"\nBoth are of same length";
}
}

void ms::operator!=(ms str)
{
for(int i=0;((*(s+i)!='\0')||(*(str.s+i)!='\0'));i++)
{
if((*(s+i))!=(*(str.s+i)))
{
cout<<"\nstring are not equal";
return;
}
}
cout<<"string are equal!!!";
}
ms ms::operator+(ms str)
{
int l;
ms tmp;
l=strlen(str.s);
l=l+strlen(s);
tmp.s=new char[l+1];
for(int i=0;*(s+i)!=0;i++)
*(tmp.s+i)=*(s+i);
int j=i;
for(i=0;*(str.s+i)!='\0';i++,j++)
*(tmp.s+j)=*(str.s+i);
*(tmp.s+j)='\0';
return(tmp);
}

void main()
{
ms m1,m2;
clrscr();
int ch;
cout<<"\nstring in first object is: ";
m1.dis();
cout<<"string in second object is: ";
m2.dis();
while(1)
{cout<<"\n1.COM OF LENGTH";
cout<<"\n2.com of equality";
cout<<"\n3.concatrena";
cout<<"\n4.exit";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nComaprison of strings";
m1>m2;
break;
case 2:
cout<<"\nequality";
m1!=m2;
break;
case 3:
cout<<"\nconcatenate is";
m1=m1+m2;
m1.dis();
break;
case 4:exit(0);
break;
}
}
 /* cout<<"\ncONCATENATED STRING IS ";
m1.dis();*/
getch();
}