实现string类的操作符重载 + = > < == != >> <<

简介: <pre code_snippet_id="596931" snippet_file_name="blog_20150203_1_6205569" name="code" class="objc">//MyString.h#pragma once#include <iostream>using namespace std;class MyString{priva
//MyString.h
#pragma  once

#include <iostream>
using namespace std;
class MyString
{
private:
	char *m_ptr;//内存空间
	
public:
	MyString(const char *str=NULL);//构造函数
	MyString(const MyString& obj); //拷贝构造函数
	~MyString();  //析构函数
public:
	// + = > < == != >> <<
	MyString operator+(const MyString& obj);  //连接
	MyString &operator=(const MyString& obj);  //赋值

	bool operator>(const MyString& obj);  //比较大小
	bool operator<(const MyString& obj);  //比较大小
	bool operator==(const MyString& obj);  //比较大小
	bool operator!=(const MyString& obj);  //比较大小

	friend istream& operator>>(istream& in, MyString &obj); //输入流
	friend ostream& operator<<(ostream& out, const MyString& obj);  //输出流
	
};
//MyString.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "MyString.h"


MyString::MyString(const char *str)//构造函数
{
	if (str == NULL)
	{
		this->m_ptr = new char[1];
		this->m_ptr[0] = '\0';
	}
	else
	{
		this->m_ptr = new char[strlen(str)+1];
		strcpy(this->m_ptr, str);
	}
}

MyString::MyString(const MyString& obj) //拷贝构造函数
{
	this->m_ptr = new char[strlen(obj.m_ptr) + 1];
	strcpy(this->m_ptr, obj.m_ptr);
}

MyString::~MyString()  //析构函数
{
	delete[] m_ptr;
	this->m_ptr = NULL;
}

MyString MyString::operator+(const MyString& obj)  //连接
{
	MyString tmp;
	delete[] tmp.m_ptr;
	tmp.m_ptr = new char[strlen(this->m_ptr) + strlen(obj.m_ptr) + 1];
	sprintf(tmp.m_ptr, "%s%s", this->m_ptr, obj.m_ptr);
	return tmp;
}

MyString &MyString::operator=(const MyString& obj)  //赋值
{
	if (this->m_ptr != NULL)
	{
		delete[] m_ptr;
		this->m_ptr = NULL;
	}
	this->m_ptr = new char[strlen(obj.m_ptr) + 1];
	strcpy(this->m_ptr, obj.m_ptr);
	return *this;
}

bool MyString::operator>(const MyString& obj)  //比较大小
{
	if (-1 == strcmp(this->m_ptr, obj.m_ptr))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool MyString::operator<(const MyString& obj) //比较大小
{
	if (1 == strcmp(this->m_ptr, obj.m_ptr))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool MyString::operator==(const MyString& obj)  //比较大小
{
	if (0 == strcmp(this->m_ptr, obj.m_ptr))
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool MyString::operator!=(const MyString& obj)  //比较大小
{
	return !(*this==obj);
}

ostream& operator<<(ostream& out, const MyString& obj) //输出流
{
	out << obj.m_ptr;
	return out;
}

istream& operator>>(istream& in, MyString &obj) //输入流
{
	char tmp[1024 * 10];
	memset(tmp, 0, sizeof(tmp));
	in >> tmp;
	if (obj.m_ptr != NULL)
	{
		delete[] obj.m_ptr;
		obj.m_ptr = NULL;
	}
	obj.m_ptr = new char[strlen(tmp) + 1];
	strcpy(obj.m_ptr, tmp);
	return in;
}

//MyStringTest.cpp
#include "MyString.h"
using namespace std;

void run()
{
	MyString s1("abd");
	MyString s2("hello");
	MyString s3 = s2;
	MyString s4;
	//s1 = s2;
	s4 = s1 + s2;
	cout << s4 << endl;
	if (s1==s2)
	{
		cout << "相等" << endl;
	}
	else
	{
		cout << "不相等" << endl;
		if (s1 < s2)
		{
			cout << "s1<s2" << endl;
		}
		else if (s1 > s2)
		{
			cout << "s1>s2" << endl;
		}
	}
	cout << s1 <<","<< s2 << endl;
	cin >> s4;
	cout << s4 << endl;
}

void main()
{
	run();
	system("pause");
}


目录
相关文章
|
28天前
|
存储 C++ 容器
C++入门指南:string类文档详细解析(非常经典,建议收藏)
C++入门指南:string类文档详细解析(非常经典,建议收藏)
37 0
|
6天前
|
存储 安全 C语言
【C++】string类
【C++】string类
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
8天前
|
编译器 C++
标准库中的string类(上)——“C++”
标准库中的string类(上)——“C++”
|
18天前
|
存储 算法 C语言
【C++初阶】8. STL初阶 + String类
【C++初阶】8. STL初阶 + String类
48 1
|
18天前
|
C语言 C++
【C++初阶】9. string类的模拟实现
【C++初阶】9. string类的模拟实现
38 1
|
1月前
|
存储 编译器 C++
string类的模拟实现
string类的模拟实现
29 0
|
1月前
|
编译器 Linux C++
string类的函数讲解
string类的函数讲解
15 1
|
1月前
|
存储 缓存 编译器
C++:String类的使用
C++:String类的使用
|
1月前
|
Java 索引
【Java】String类常用方法总结
【Java】String类常用方法总结
20 0