JustPaste.it

String function with friend Show function that calls destructor

#include<iostream>
using namespace std;
#include<string.h>

class String
{

public:
char *p;
int len;
String()
{
cout << "empty constructor" << endl;
len=0;
//p=0;
}
String(const char *s) {
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
/*
String(const String &s)
{
len=s.len;
p=new char[len+1];
strcpy(p,s.p);
}
*/
friend String operator+(const String&s, const String&t);
friend int operator<=(const String&s, const String&t);
friend void show(const String s);
// String operator=(const String s){
// p = s.p;
// len = s.len;
// }

~String() {delete p;}
};

String operator+(const String &s, const String &t)
{
String temp;
temp.len=(s.len-1)+t.len;
temp.p = new char[temp.len];
strcpy(temp.p,s.p);
strcat(temp.p,t.p);
cout << temp.p << endl;
return(temp);
}

int operator<=(const String &s, const String &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if (m<=n)
return(1);
else
return(0);
}


void show(const String s)
{
cout<<s.p<<endl;
}


int main()
{
String s1="New ";
String s2="York";
String s3="Delhi";
String string1,string2;

string1=s1;
string2=s2;
String string3=s1+s3;

show(string1);
show(string2);
show(string3);

if(string1<=(string3))
{
show(string1);
cout<<"is smaller.";
}
else
{
show(string3);
cout<<"is smaller.";
}
return 0;
}