以下程序是一个具有代表和常用的一些运算符重载小例子。
#include<iostream>
#include<cstring>
#include<cstring>
using namespace std;
class Mystring
{
private:
int m_length;
char *m_data;
public:
Mystring();
Mystring(char *str);
Mystring(int a,char ch);
~Mystring();
Mystring(const Mystring &s);
friend ostream &operator <<(ostream &out,const Mystring &s);
friend istream &operator >>(istream &in, Mystring &s);
Mystring &operator =(char *str);
Mystring &operator =(Mystring &s);
char &operator [](int index);
};
{
private:
int m_length;
char *m_data;
public:
Mystring();
Mystring(char *str);
Mystring(int a,char ch);
~Mystring();
Mystring(const Mystring &s);
friend ostream &operator <<(ostream &out,const Mystring &s);
friend istream &operator >>(istream &in, Mystring &s);
Mystring &operator =(char *str);
Mystring &operator =(Mystring &s);
char &operator [](int index);
};
Mystring::Mystring()
{
m_length=0;
m_data=NULL;
}
{
m_length=0;
m_data=NULL;
}
Mystring::Mystring(char *str)
{
m_length=strlen(str);
m_data=new char[m_length+1];
strcpy(m_data,str);
m_data[m_length]='\0';
}
{
m_length=strlen(str);
m_data=new char[m_length+1];
strcpy(m_data,str);
m_data[m_length]='\0';
}
Mystring::Mystring(int a,char ch)
{
m_length=a;
m_data=new char[a+1];
for(int i=0;i<m_length;i++)
{
m_data[i]=ch;
}
m_data[m_length]='\0';
}
Mystring::~Mystring()
{
if(m_data != NULL)
delete[] m_data;
m_data=NULL;
}
{
if(m_data != NULL)
delete[] m_data;
m_data=NULL;
}
Mystring::Mystring(const Mystring &s)
{
m_length=s.m_length;
m_data=new char[m_length+1];
strcpy(m_data,s.m_data);
m_data[m_length]='\0';
}
{
m_length=s.m_length;
m_data=new char[m_length+1];
strcpy(m_data,s.m_data);
m_data[m_length]='\0';
}
ostream &operator <<(ostream &out,const Mystring &s)
{
out<<s.m_data;
return out;
}
{
out<<s.m_data;
return out;
}
istream &operator >>(istream &in, Mystring &s)
{
char temp[100]={0};
in>>temp;
s.m_length=strlen(temp);
s.m_data=new char[s.m_length+1];
strcpy(s.m_data,temp);
s.m_data[s.m_length]='\0';
return in;
}
{
char temp[100]={0};
in>>temp;
s.m_length=strlen(temp);
s.m_data=new char[s.m_length+1];
strcpy(s.m_data,temp);
s.m_data[s.m_length]='\0';
return in;
}
Mystring &Mystring::operator =(char *str)
{
m_length=strlen(str);
if(m_data != NULL)
delete[] m_data;
m_data=new char[m_length+1];
strcpy(m_data,str);
m_data[m_length]='\0';
return *this;
}
{
m_length=strlen(str);
if(m_data != NULL)
delete[] m_data;
m_data=new char[m_length+1];
strcpy(m_data,str);
m_data[m_length]='\0';
return *this;
}
Mystring &Mystring::operator =(Mystring &s)
{
m_length=s.m_length;
if(m_data != NULL)
delete[] m_data;
m_data=new char[m_length+1];
strcpy(m_data,s.m_data);
m_data[m_length]='\0';
return *this;
}
char &Mystring::operator [](int index)
{
return m_data[index];
}
int main()
{
Mystring s1=("hello");
cout<<"s1:"<<s1<<endl;
Mystring s2(10,'a');
cout<<"s2:"<<s2<<endl;
Mystring s3(s1);
cout<<s3<<endl;
Mystring s4=("hello whold");
s1=s4;
cout<<s1<<endl;
s1=s4;
return 0;
}
{
Mystring s1=("hello");
cout<<"s1:"<<s1<<endl;
Mystring s2(10,'a');
cout<<"s2:"<<s2<<endl;
Mystring s3(s1);
cout<<s3<<endl;
Mystring s4=("hello whold");
s1=s4;
cout<<s1<<endl;
s1=s4;
return 0;
}