#include
#include
/*
Name:
Copyright: Institute of Computer Network & Communication Technology(CCNU)
Author: ccnupq
Date: 07-10-07 13:56
Description:
*/
using namespace std;
/*! @class
********************************************************************************
类名:String
功能:实现String 类的构造函数、析构函数、拷贝构造函数、赋值函数,字符串相连函数等等
*******************************************************************************/
class String
{
public:
String(const char *str=NULL);//构造函数
String(const String &);//拷贝构造函数
~String(void);//析构函数
String& operator=(const String &);//重载"=",字符转赋值
String& operator+(const String &other);//,重载"+",字符串链接
String& operator-(const String &other);//,重载"-",删除字串
bool operator==(const String &other);//重载"==",判断两个字符串相等
bool operator<(const String &other);
bool operator>(const String &other);
//重载<<,这里必须使用friend,因为涉及到两个类之间私有成员的访问
friend ostream& operator << (ostream &, const String &);
private:
char *m_data;
};
//构造函数
String::String(const char *str)
{
if(str==NULL)
{//无参数,默认构造一个空串
m_data=new char[1];
*m_data='\0';
cout<<"调用了默认构造函数"<
else
{//有参数,
int length=strlen(str);
m_data=new char[length+1];
strcpy(m_data,str);
cout<<"调用了普通构造函数"<
}
//拷贝构造函数,用一个对象初始化另外一个对象的时候调用
String::String(const String &other)
{
int length=strlen(other.m_data);
m_data=new char[length+1];
strcpy(m_data,other.m_data);
cout<<"调用了拷贝构造函数"<
//析构函数
String::~String()
{
delete[] m_data;
}
//重载运算符"=" ,用一个对象赋值给另外一个对象的时候调用
String& String::operator=(const String &other)
{
if (this==&other)
return *this;
delete [] m_data;
int length=strlen(other.m_data);
m_data=new char[length+1];
strcpy(m_data,other.m_data);
cout<<"重载了运算符="<
return *this;
}
////重载运算符"+"
String& String::operator+(const String &other)
{
char temp[100];
strcpy(temp,m_data);
int one_length=strlen(m_data);
int other_length=strlen(other.m_data);
delete[] m_data;
m_data=new char[one_length+other_length+1];
strcpy(m_data,temp);
strcat(m_data,other.m_data);
cout<<"重载了运算符+"<
return *this;
}
String& String::operator-(const String &other)
{
char *temp,*p;
if( (temp=strstr(m_data,other.m_data)) ==NULL)
{
cout<<"没有符合的子串,不能使用'-'操作"<
}
else
{
p=temp;
temp=temp+strlen(other.m_data);
*p='\0';
strcat(m_data,temp);
}
cout<<"重载了运算符-"<
}
//重载运算符"<<"
ostream& operator<<(ostream &output, const String &other)
{
output<
cout<<"重载了运算符<<"<
return output;
}
bool String::operator<(const String &other)
{
if(strcmp(m_data,other.m_data)<0)
return true;
return false;
}
bool String::operator>(const String &other)
{
if(strcmp(m_data,other.m_data)>0)
return true;
return false;
}
bool String::operator==(const String &other)
{
if(strcmp(m_data,other.m_data)==0)
return true;
return false;
}
/*! @function
********************************************************************************
函数名:main
*******************************************************************************/
int main()
{
String s1("Hello,");
String s2("World!");
String s3(s2);//调用拷贝构造函数
String s4(s2);
String s5("Hello,World");
String s6("Hello,World!");
String s7("Hello,World!");
String s8("o,W");
String s9;
//String s3;//调用构造函数中的默认构造.
//s3=s2;// 调用重载后的赋值函数
cout<
s3=s1+s2;
cout<
if(s4==s2)
cout<<"两个字符串相等"<
cout<<"两个字符串不相等"<
if(s5
cout<<"s5大于s6"<
cout<<"s5等于s6"<
s9=s7-s8;
cout<
system("pause");
}
string& operator =(string& b)
=================偶是分割线(* ̄▽ ̄*)=================
返回值的string&与函数参数里的string&使用引用(&)的原因是不同的。
返回值使用引用是为了函数的多层次调用。
比如
string a;
string b;
string c;
a=b=c;//使用返回值为string&的重载条符可以
因为a=b返回值其实是a对象的引用,所以,可以再次调用=操作符。另一方面也有有参数使用引用同样的原因,如下。
=================偶是分割线(* ̄▽ ̄*)=================
参数里使用引用的原因是,C/C++中,参数都是按值传递的,除非使用引用(或指针)。也就是说不管什么样的参数,都是复制一份参数的值创建临时对象,然后函数使用这些临时对象。这个显而易见,比如
#include
void nagitive(int n)
int main()
{
int a=1;
nagitive(a);
std::cout< return 0;
}
由于是按值传递,所以函数改变的其实是复制的临时对象。
然而,复制参数有时候代价是巨大的,尤其是结构很大的类对象,更重要的是某些对象没有复制构造函数,不能复制,比如std::iostream流对象std::cin,std::cout。这时候需要使用引用,避免了拷贝对象的昂贵代价,或者不能拷贝参数的困境。
另外,团IDC网上有许多产品团购,便宜有口碑