/*Operator +(concat) -(reverse) for string. Create objects using dynamic constructor*/
#include
#include
#include
class string
{
private:
char *ch;
public:
string()
{
//default constructor
}
//dynamic constructor1
string (char *a)
{
int len=strlen(a);
ch=new char[len+1];
strcpy(ch,a);
}
//dynamic constructor2
string (char *a,char *b)
{
int len=strlen(a);
int len1=strlen(b);
ch=new char[len+len1+1];
}
void show()
{
cout<
}
string operator+(string &);
void operator-();
};//end class string
string string::operator+(string &B)
{
string C(this->ch,B.ch);
strcpy(C.ch,ch);
strcat(C.ch,B.ch);
return C;
}
void string::operator-()
{
strrev(this->ch);
}
void main()
{
clrscr();
int ch;
cout<<"\n1:String concatination\n2:String reverse\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
char a[10],b[10];
cout<<"\nEnter first string:";
cin>>a;
cout<<"\nEnter second string:";
cin>>b;
string A(a),B(b),C(a,b);
C=A+B;
cout<<"\nFirst string is:";
A.show();
cout<<"\nSecondstring is:";
B.show();
cout<<"\nConcatinated string is:";
C.show();
break;
case 2:
char d[10];
cout<<"\nEnter your string:";
cin>>d;
string D(d);
-D;
cout<<"\nReversed string is:";
D.show();
break;
default:
cout<<"\n\n\t\tWRONG CHOICE";
}//end choice;
getch();
}//end main
Copyright © 2011 - All Rights Reserved - Softron.in
Template by Softron Technology