#include "stdafx.h"
#include<iostream>
#include<string.h>
using namespace std;
//类的成员函数中,有一些是加了const修饰符的,表示这个函数不会对类的成员进行修改,一些函数的输入参数也加了const修饰,表示该函数不会改变这个参数的值
class String
{
public:
String(const char *str = NULL); //通用构造函数,没值的时候,用NULL默认初始化
String(const String &str); //拷贝构造函数
~String(); //析构函数
String operator+(const String &str)const; //重载+
String& operator=(const String &str); //重载=
String& operator+=(const String &str);
bool operator==(const String &str)const; //重载==
char& operator[](int n)const; //重载[]
size_t size()const; //获取长度
const char* c_str()const; //获取C字符串
friend std::istream& operator>>(std::istream&is, String &str); //输入
friend std::ostream& operator<<(std::ostream&os, String &str); //输出
private:
char *data; //字符串
size_t length; //长度
};
String::String(const char *str) // 通用构造函数
{
if (!str)
{
length = 0;
data = new char[1];
*data = '\0';
}
else
{
length = strlen(str);
data = new char[length + 1];
strcpy_s(data, length+1,str);
}
}
String::String(const String &str) //拷贝构造函数,要深层复制
{
length = str.size();
data = new char[length + 1];
strcpy_s(data,str.length+1, str.c_str());
}
String::~String()
{
delete[] data;
length = 0;
}
String String::operator+(String const &str)const
{
String newString;
newString.length = length + str.size();
newString = new char[newString.length + 1];
strcpy_s(newString.data,newString.length+1, data);
strcat_s(newString.data,newString.length+1, str.data);
return newString;
}
String& String::operator=(const String &str) //链式赋值,返回引用(对象本身)
{
if (this == &str)
return *this;
delete[]data;
length = str.length;
data = new char[length + 1];
strcpy_s(data,length+1, str.data);
return *this;
}
String& String::operator+=(const String &str)
{
length += str.length;
char *newData = new char[length + 1];
strcpy_s(newData,length+1, data);
strcat_s(newData,length+1, str.data);
delete[]data;
data = newData;
return *this;
}
inline bool String::operator==(const String&str)const
{
/*if (length != str.length)
return false;*/
return strcmp(data, str.data) ? false : true;
}
inline char& String::operator[](int n)const
{
if (n >= length)
return data[length - 1];
else
return data[n];
}
inline size_t String::size()const
{
return length;
}
//重载输入运算符,先申请一块足够大的内存来存放输入字符串,再进行新的字符串的生成
std::istream& operator>>(std::istream& is, String &str) //为了连续输入,返回引用
{
char tem[100];
is >> tem;
str.length = strlen(tem);
str.data = new char[str.length + 1];
strcpy_s(str.data,str.length+1, tem);
return is;
}
std::ostream&operator<<(std::ostream &os, String &str) //为了连续输出,返回引用(对象本身)
{
os << str.data;
return os;
}
inline const char* String::c_str()const
{
return data;
}
int _tmain(int argc, _TCHAR* argv[])
{
/*String s;
std::cin >> s;
printf("s:is\n%s\ns.size()is\n%d\n",s,s.size());*/
//std::cout << "s:\n" << s<<"\n" << "s.size()\n" << s.size() << std::endl;
char a[] = "hello", b[] = "world";
String s1(a), s2(b);
//printf("s1\n%c\ns2\n%c\n", s1, s2);
// std::cout << s1 << "+" << s2 << "=" << s1 + s2 << std::endl;
String s3 = s1 + s2;
if (s1 == s3)
cout << "first:s1==s3" << endl;
s1 += s2;
if (s1 == s3)
cout << "second:s1==s3" << endl;
return 0;
}
参看:https://www.cnblogs.com/zhizhan/p/4876093.html