主题:运算符重载问题
编译通过了,连接报两个错。。
#include<iostream.h>
#include<string.h>
class String
{
friend bool operator <=(String &str1, String &str2);
friend bool operator <(String &str1, String &str2);
friend bool operator >(String &str1, String &str2);
friend bool operator ==(String &str1, String &str2);
friend bool operator >=(String &str1, String &str2);
friend bool operator !=(String &str1, String &str2);
public:
String() {_str = NULL;} //默认构造函数
String(char *str); //带有一个参数的构造函数
String(String &src); //拷贝构造函数
~String() {delete[] _str;} //析构函数
void Print() const; //输出字符串
int GetLength() const {return strlen(_str);}//取字符串长度
char & operator [](int index)const;//访问字符串的某一字符
String & operator =(const String &src);//字符串赋值
String operator +(const String &src);//字符串连接
private:
char *_str;
};
String::String(char *str)
{
_str = new char[strlen(str)+1]; //动态申请空间
strcpy(_str, str);
}
String::String(String &src)
{
_str = new char[strlen(src._str)+1]; //动态申请空间
strcpy(_str, src._str);
}
void String::Print() const
{
cout << _str << endl;
}
bool operator <=(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) <= 0)
{
return true;
}
else
return false;
}
bool operator >(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) > 0)
{
return true;
}
else
{
return false;
}
}
bool operator ==(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) == 0)
{
return true;
}
else
{
return false;
}
}
bool operator >=(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) >= 0)
{
return true;
}
else
return false;
}
bool operator !=(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) != 0)
{
return true;
}
else
{
return false;
}
}
//访问字符串的某一个字符
char & String::operator [](int index)const
{
return _str[index];
}
//字符串赋值
String & String::operator =(const String &src)
{
delete _str;
_str = new char[src.GetLength()+1];
strcpy(_str,src._str);
return *this;
}
//连接字符串
String String::operator +(const String &src)
{
char *save;
save = new char[strlen(_str)+strlen(src._str)+1];
strcpy(save, _str);
strcpy(save+strlen(_str),src._str);
return String(save);
}
int main()
{
String str1("hello");
String str2("world");
String str3(str1);
cout << "str1:" , str1.Print();
cout << "str2:" , str2.Print();
//测试比较运算符
cout << "str1 < str2 = " << (str1 < str2) << endl;
cout << "str1 <= str2 = " << (str1 <= str2) << endl;
cout << "str1 == str = " << (str1 == str2) << endl;
cout << "str1 > str2 = " << (str1 > str2) << endl;
cout << "str1 >= str2 = " << (str1 >= str2) << endl;
//测试赋值操作符
str3 = str1;
cout << "str3 = str1, str3:" , str3.Print();
//测试+运算符
str3 = (str3 + str2);
cout << "str3 += str2, str3:" , str3.Print();
//测试下标操作符
cout << "str3[3] = " << str3[3] << endl;
return 0;
}
#include<iostream.h>
#include<string.h>
class String
{
friend bool operator <=(String &str1, String &str2);
friend bool operator <(String &str1, String &str2);
friend bool operator >(String &str1, String &str2);
friend bool operator ==(String &str1, String &str2);
friend bool operator >=(String &str1, String &str2);
friend bool operator !=(String &str1, String &str2);
public:
String() {_str = NULL;} //默认构造函数
String(char *str); //带有一个参数的构造函数
String(String &src); //拷贝构造函数
~String() {delete[] _str;} //析构函数
void Print() const; //输出字符串
int GetLength() const {return strlen(_str);}//取字符串长度
char & operator [](int index)const;//访问字符串的某一字符
String & operator =(const String &src);//字符串赋值
String operator +(const String &src);//字符串连接
private:
char *_str;
};
String::String(char *str)
{
_str = new char[strlen(str)+1]; //动态申请空间
strcpy(_str, str);
}
String::String(String &src)
{
_str = new char[strlen(src._str)+1]; //动态申请空间
strcpy(_str, src._str);
}
void String::Print() const
{
cout << _str << endl;
}
bool operator <=(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) <= 0)
{
return true;
}
else
return false;
}
bool operator >(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) > 0)
{
return true;
}
else
{
return false;
}
}
bool operator ==(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) == 0)
{
return true;
}
else
{
return false;
}
}
bool operator >=(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) >= 0)
{
return true;
}
else
return false;
}
bool operator !=(String &str1, String &str2)
{
if (strcmp(str1._str,str2._str) != 0)
{
return true;
}
else
{
return false;
}
}
//访问字符串的某一个字符
char & String::operator [](int index)const
{
return _str[index];
}
//字符串赋值
String & String::operator =(const String &src)
{
delete _str;
_str = new char[src.GetLength()+1];
strcpy(_str,src._str);
return *this;
}
//连接字符串
String String::operator +(const String &src)
{
char *save;
save = new char[strlen(_str)+strlen(src._str)+1];
strcpy(save, _str);
strcpy(save+strlen(_str),src._str);
return String(save);
}
int main()
{
String str1("hello");
String str2("world");
String str3(str1);
cout << "str1:" , str1.Print();
cout << "str2:" , str2.Print();
//测试比较运算符
cout << "str1 < str2 = " << (str1 < str2) << endl;
cout << "str1 <= str2 = " << (str1 <= str2) << endl;
cout << "str1 == str = " << (str1 == str2) << endl;
cout << "str1 > str2 = " << (str1 > str2) << endl;
cout << "str1 >= str2 = " << (str1 >= str2) << endl;
//测试赋值操作符
str3 = str1;
cout << "str3 = str1, str3:" , str3.Print();
//测试+运算符
str3 = (str3 + str2);
cout << "str3 += str2, str3:" , str3.Print();
//测试下标操作符
cout << "str3[3] = " << str3[3] << endl;
return 0;
}