#include<iostream>
#include<cstring>
#include<new>
using namespace std;
class string_class
{
char *p;
int len;
public:
========== Empty Constructor ==========
string_class()
{
cout<<"000000000000000000000000000000000\n";
}
========== Single Parameterised Constructor ==========
string_class(const char *str)
{
cout<<"see..in single constructor arg char*...\n";
len=strlen(str);
p=new char[len+1];
strcpy(p,str);
cout<<"see.....\n";
}
string_class(const string_class& str1,const string_class& str2)
{
cout<<"string_class(const string_class& str1,const string_class& str2)"<<"\n";
int len1,len2;
len1=strlen(str1.p);
cout<<"in str1 str2="<<'\n';
len2=strlen(str2.p);
cout<<"len1,len2 is "<<len1<<" "<<len2<<"\n";
cout<<str1.p<<" "<<str2.p<<"\n";
cout<<len1+len2+1<<"\n";
//delete p;
len=len1+len2;
//delete p;
p=(char*)new char[(len+1)];
cout<<"success"<<'\n';
cout<<len1+len2+1<<'\n';
strcpy(p,str1.p);
cout<<p<<"kkk"<<'\n';
strcpy((p+len1),str2.p);
//p9[len1+len2]='\0';
cout<<p<<'\n';
}
==================================================
string_class(const string_class& str1,const char *str2)
{
cout<<"string_class(const string_class& str1,const char *str2)"<<"\n";
int len1,len2;
len1=strlen(str1.p);
len2=strlen(str2);
//delete p;
p=new char[len1+len2+1];
strcpy(p,str1.p);
strcpy((p+len1),str2);
}
==================================================
string_class(const char *str1,const string_class& str2)
{
cout<<"string_class(const char *str1,const string_class& str2)"<<"\n";
int len1,len2;
len1=strlen(str1);
len2=strlen(str2.p);
cout<<"in str1 str2="<<len1<<len2<<"\n";
//delete p;
p=new char[len1+len2+1];
strcpy(p,str1);
strcpy((p+len1),str2.p);
}
==================================================
string_class(const string_class& str1, char ch2)
{
cout<<"string_class(const string_class& str1,const char ch2)"<<'\n';
int len1;
len1=strlen(str1.p);
cout<<"in str1, ch2="<<len1<<"\n";
//delete p;
p=new char[len1+2];
strcpy(p,str1.p);
strcpy((p+len1),&ch2);
p[len1+1]='\0';
cout<<p<<'\n';
}
==================================================
string_class(char ch1,const string_class& str2)
{
cout<<"string_class(const char ch1,const string_class& str2)"<<"\n";
int len2;
len2=strlen(str2.p);
cout<<"in ch1 str2="<<len2<<"\n";
//delete p;
p=new char[len2+2];
strcpy(p,&ch1);
cout<<p<<'\n';
strcpy((p+1),str2.p);
}
==================================================
friend ostream& operator<<(ostream& os, const string_class& str)/////////////////////for object overload cout//////////global friend function///////////
{
os << str.p ;
return os;
}
friend istream& operator>>(istream& os,const string_class& str)/////////////////////for object overload cout//////////global friend function///////////
{os>> str.p;
return os;
}
friend void swaap(string_class& str1,string_class& str2)
{
string_class str3;
str3=str1;
str1=str2;
str2=str3;
}
friend string_class operator+(const string_class& str1,const string_class& str2)
{
cout<<" friend string_class operator+(const string_class& str1, const string_class& str2)"<<'\n';
string_class temp0(str1,str2);
return temp0;
}
==================================================
friend string_class operator+ (const char *str1, const string_class& str2)
{
cout<<"friend string_class operator+ (const char *str1, const string_class& str2)"<<"\n";
string_class temp1(str1,str2);
return temp1;
}
==================================================
friend string_class operator+ (const string_class& str1, const char *str2)
{
cout<<"friend string_class operator+ (const string_class& str1, const char *str2)"<<"\n";
string_class temp2(str1,str2);
cout<<"in str1,str2 funct\n";
return temp2;
}
==================================================
friend string_class operator+ (char ch1, const string_class& str2)
{
cout<<"friend string_class operator+ (const char ch1, const string_class& str2)"<<"\n";
string_class temp3(ch1,str2);
return temp3;
}
==================================================
friend string_class operator+ (const string_class& str1,char ch2)
{
cout<<"friend string_class operator+ (const string_class& str1,const char ch2)"<<"\n";
string_class temp4(str1,ch2);
cout<<"in str1,ch2 funct\n";
return temp4;
}
};
==================================================
main()
{
cout<<"main\n";
string_class str2("shiva");
string_class str4("kumaraswamy ");
string_class str1("ganapathi");
string_class str3,str5;
str3=str2+'U'+str4;
str5=str4;
//ganapathiTshiva
cout<<"in main......................."<<str3;
}
Implementing userdefined string class
Not working in case of str3=str4+'U'+str2;
Getting segmentation fault. Try It Out!
http://ideone.com/
ReplyDeletei checked in this editor it was working fine without getting any segmentation fault..
can u mention the environment?