自定义一个 string 类 , 要求添加相关函数的实现代码 。
class String
{
public:
String(const string (s); / / 构造函数
/ / 友元运算符重载
friend String operator +(const String &s1 const String &s2);
friend bool operator ==(const String &s1 ,const String &s2);
/ / 成员函数运算符
bool operator !=(const String &other);
/ / 类型转换重载
String& operator =(const String &s);
operator string(); / / 类型重载
/ / 成员函数
string get_str();
private:
string str;
}
#include <iostream>
#include <string>
using namespace std;
class String {
public:
String(const string& s) : str(s) {} // 构造函数
// 友元运算符重载
friend String operator +(const String& s1, const String& s2);
friend bool operator ==(const String& s1, const String& s2);
// 成员函数运算符
bool operator !=(const String& other);
// 类型转换重载
String& operator =(const String& s);
o