0% found this document useful (0 votes)
9 views2 pages

Custom String Class in C++ Code

This C++ program defines a mystring class to represent strings. The mystring class includes operators to assign strings from char, char arrays, and other mystring objects. It also includes operators to compare mystring objects and a display method. The main function demonstrates assigning and comparing mystring objects and using the if/else conditional.

Uploaded by

Hamza Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Custom String Class in C++ Code

This C++ program defines a mystring class to represent strings. The mystring class includes operators to assign strings from char, char arrays, and other mystring objects. It also includes operators to compare mystring objects and a display method. The main function demonstrates assigning and comparing mystring objects and using the if/else conditional.

Uploaded by

Hamza Ali
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<iostream>

#include<string.h>
using namespace std;

class mystring
{
public :
char *arr;

mystring()
{
arr=NULL;
cout<<"I am Constructor"<<endl;
}
void operator=(char ch)
{
if(arr!=NULL)
{
free(arr);
}
arr=(char*)malloc(2);

arr[0]=ch;
arr[1]='\0';
}
void operator=(char *t)
{
if(arr!=NULL)
{
free(arr);
}
int l1=strlen(t);
arr=(char*)malloc(l1+1);
strcpy(arr,t);
}
void operator=(mystring scopy)
{
if(arr!=NULL)
{
free(arr);
}
int l1=strlen([Link]);
arr=(char*)malloc(l1+1);
strcpy(arr,[Link]);
}

int operator>(mystring s)
{
int d;
d=strcmp(arr,[Link]);
if(d>0)
{
return 1;
}
else
{
return 0;
}
}

int operator<(mystring s)
{
int d;
d=strcmp(arr,[Link]);
if(d<0)
{
return 1;
}
else
{
return 0;
}
}
void display()
{
puts(arr);
}

};

void main()
{
mystring s1,s2;
s1="Sahibzada Muhammad Shahid Khan Afridi "; //char *t
[Link]();
s1='a'; //char ch
[Link]();
s2="Ali"; //mystring temp
[Link]();
if(s1>s2)
{
s1="If Condition";
}
else
{
s1="else Condition";
}
[Link]();

You might also like