[面试题][C++] string类实现

#include<iostream>

using namespace std;
//练习C++中构造函数,拷贝构造函数,赋值函数

class String{
private:
	char *m_string;
public:
	//构造函数
	String(const char *str = NULL);
	/*拷贝构造函数----入参必须为引用,不为值传递是为了防止拷贝构造函数的无限递归,最终导致栈溢出
	1)一个对象以值传递的方式传入函数体
	2)一个对象以值传递的方式从函数返回
	3)一个对象需要通过另一个对象进行初始化
	*/
	String(const String &other);
	//赋值函数----入参最好为引用,值传递会多一次拷贝
	String& operator=(const String &other);
	//析构函数
	~String();

	char& operator[](int index);
	bool operator==(const String &other);
	String& operator+=(const String &other);
	String operator+(const String &other);//注意返回值为值传递非引用,因为返回的是函数局部变量,若为引用,退出函数时,空间已被释放

	//a << cout; 连续输出 b<<(a << cout);
	//cout << a;
	ostream& operator<<(ostream &out);
	friend ostream& operator<<(ostream &out,String &s);
	friend istream& operator>>(istream &in, String& s);
};
//------------------------------------------
String::String(const char *str){
	cout << "constructor" << endl;
	if (NULL == str){//如果str为空就存储一个空字符串""
		m_string = new char[1];
		*m_string = '\0';
	}
	else{
		m_string = new char[strlen(str) + 1];
		strcpy(m_string, str);
	}
}
String::String(const String &other){
	cout << "copy constructor" << endl;
	m_string = new char[strlen(other.m_string) + 1];
	strcpy(m_string, other.m_string);
}
String& String::operator=(const String &other){
	cout << "assignment operator" << endl;
	if (this == &other){
		return *this;
	}
	delete[]m_string;//先释放原来的内存
	m_string = new char[strlen(other.m_string) + 1];
	strcpy(m_string, other.m_string);
	return *this;
}
String::~String(){
	cout << "deconstructor" << endl;
	if (NULL != m_string){
		delete[]m_string;
		m_string = NULL;
	}
}
char& String::operator[](int index){
	return m_string[index];
}
bool String::operator==(const String &other){
	if (strcmp(m_string, other.m_string) == 0)
		return true;
	return false;
}
String& String::operator+=(const String &other){
	char *tmp = m_string;
	m_string = new char[strlen(m_string) + strlen(other.m_string) + 1];
	strcpy(m_string, tmp);
	delete[]tmp;
	strcat(m_string, other.m_string);
	return *this;
}
String String::operator+(const String &other){
	String ret;
    delete[]ret.m_string;
	ret.m_string = new char[strlen(m_string) + strlen(other.m_string) + 1];
	strcpy(ret.m_string, m_string);
	strcat(ret.m_string, other.m_string);
	return ret;
}
ostream& operator<<(ostream &out, String &s){
	out <<"MyString by frined: " << s.m_string << endl;
	return out;
}
ostream& String::operator<<(ostream &out){
	out << "MyString : " << m_string << endl;
	return out;
}
istream& operator>>(istream &in, String& s){
	//in >> s.m_string;
	char tmp[1024];
	in.getline(tmp, 1024);
	s = tmp;
	return in;
}
//----------------------------------------
int main(){
	String a("hello ");//调用构造函数
	String b("world ");
	String c(a);//调用拷贝构造函数
	c = b;//调用赋值函数
	cout << a;
	cout << a[1] << endl;
	cout << (c == b) << endl;
	c += a;
	cout << c;
	cout << (a + b);
	//cin >> b;
	//cout << b;
	return 0;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值