Working with Strings using constructor overloading


//String operations using overloading constructors

#include<iostream.h>

#include<conio.h>

#include<string.h>

class string

{

char *p;

int len;

public:

string()

{

len=0;p=0;

}

string(const char *s);

string(const char &s);

string(const string &s);

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::string(const char *s)

{

len=strlen(s);

p=new char[len+1];

strcpy(p,s);

}

string::string(const string &s)

{

len=s.len;

p=new char[len+1];

strcpy(p,s.p);

}

string operator +(const string &s,const string &t)

{

string temp;

temp.len=s.len+t.len;

temp.p=new char[temp.len+1];

strcpy(temp.p,s.p);

strcat(temp.p,t.p);

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;

}

void main()

{

string s1=”Chandra”;

string s2=”Sujatha”;

string s3=” Sundarraj”;

string t1,t2,t3;

clrscr();

cout<<“\n\t\t String operations using constructor overloading”;

cout<<“\n\t\t ~~~~~~ ~~~~~~~~~~ ~~~~~ ~~~~~~~~~~~ ~~~~~~~~~~~”;

t1=s1;

t2=s2;

t3=s1+s3;

cout<<“\n\n OUTPUT:”;

cout<<“\n ~~~~~~~”;

cout<<“\n t1=”;

show(t1);

cout<<“\n t2=”;

show(t2);

cout<<“\n t3=”;

show(t3);

cout<<“\n\n”;

if(t1<=t3)

{

show(t1);

cout<<” is smaller than “;

show(t3);

cout<<endl;

}

else

{

show(t3);

cout<<” is smaller than “;

show(t1);

}

getch();

}

Advertisements

One thought on “Working with Strings using constructor overloading

  1. upload more programs in visual programaming.

    and also 2marks and previous year question paper “(anna university) for qrid computing if possible for all subs

Comments are closed.