操作符重载专题强化训练-自定义字符串类

#pragma once
#include <iostream>
using namespace std;
class MyString
{
public:
	MyString();
	MyString(int len);
	MyString(const char*p);
	MyString(const MyString& obj);
	~MyString(void);

	void print();
	int getLen();

	MyString& operator=(const char *p);
	MyString& operator=(const MyString &obj);

	char& operator[](int index) const;

	bool operator==(const char* p) const;
	bool operator!=(const char* p) const;
	bool operator==(const MyString& obj) const;
	bool operator!=(const MyString& obj) const;

	bool operator<(const char* p) const;
	bool operator>(const char* p) const;
	bool operator<(const MyString &obj) const;
	bool operator>(const MyString &obj) const;

private:
	int m_len;
	char *m_p;
	friend ostream& operator<<(ostream &out, const MyString &obj);
	friend istream& operator>>(istream &in,  MyString &obj);

};


#include "MyString.h"
#include <iostream>
using namespace std;

MyString::MyString()
{	
	//初始化为空串
	this->m_len = 0;
	this->m_p = new char[m_len+1];
	strcpy(m_p,"");
}

MyString::MyString(int len)
{	
	if (len < 0)
	{
		this->m_len = 0;
		this->m_p = new char[m_len+1];
		strcpy(m_p,"");
	}
	this->m_len = len;
	this->m_p = new char[m_len+1];
}
MyString::MyString(const char*p)
{
	if (p==NULL)
	{
		this->m_len = 0;
		this->m_p = new char[m_len+1];
		strcpy(m_p,"");
	}
	this->m_len = strlen(p);
	this->m_p = new char[m_len+1];
	strcpy(this->m_p,p);
}

MyString::MyString(const MyString& obj)
{
	this->m_len = obj.m_len;
	this->m_p = new char[m_len+1];
	strcpy(this->m_p,obj.m_p);
}

MyString::~MyString(void)
{
	if (this->m_p!=NULL)
	{
		delete[] this->m_p;
		this->m_p = NULL;
		this->m_len =0;
	}
}

void MyString::print()
{
	cout<<this->m_p<<endl;
}
int MyString::getLen()
{
	return this->m_len;
}

MyString& MyString::operator=(const char *p)
{
	if (this->m_p!=NULL)
	{
		delete[] this->m_p;
		this->m_p = NULL;
		this->m_len =0;
	}
	if(p==NULL)
	{
		this->m_len = 0;
		this->m_p = new char[m_len+1];
		strcpy(m_p,"");
		return *this;
	}
	this->m_len = strlen(p);
	this->m_p = new char[m_len+1];
	strcpy(this->m_p,p);
	return *this;
}

MyString& MyString::operator=(const MyString &obj)
{
	if (this->m_p!=NULL)
	{
		delete[] this->m_p;
		this->m_p = NULL;
		this->m_len =0;
	}
	this->m_len = obj.m_len;
	this->m_p = new char[m_len+1];
	strcpy(this->m_p,obj.m_p);
	return *this;
}

char& MyString::operator[](int index) const
{
	return this->m_p[index];
}

bool MyString::operator==(const char* p) const
{
	if (p==NULL)
	{
		if (m_len==0)
		{
			return true;
		}
		return false;
	}
	if (strcmp(this->m_p,p) == 0)
	{
		return true;
	}
	return false;
}
bool MyString::operator!=(const char* p) const
{
	return !(*this==p);
}

bool MyString::operator==(const MyString& obj) const
{
	if (this->m_len != obj.m_len)
	{
		return false;
	}
	if (strcmp(this->m_p,obj.m_p) != 0)
	{
		return false;
	}
	return true;
}
bool MyString::operator!=(const MyString& obj) const
{
	return !(*this == obj);
}

bool MyString::operator<(const char* p) const
{
	if (strcmp(this->m_p,p) < 0)
	{
		return true;
	}
	return false;
}
bool MyString::operator>(const char* p) const
{
	if (strcmp(this->m_p,p) > 0)
	{
		return true;
	}
	return false;
}

bool MyString::operator<(const MyString &obj) const
{
	if (strcmp(this->m_p,obj.m_p) < 0)
	{
		return true;
	}
	return false;
}

bool MyString::operator>(const MyString &obj) const
{
	if (strcmp(this->m_p,obj.m_p) > 0)
	{
		return true;
	}
	return false;
}

ostream& operator<<(ostream &out, const MyString &obj)
{
	out<<obj.m_p;
	return out;
}

istream& operator>>(istream &in, MyString &obj)
{
	in>>obj.m_p;
	return in;
}

#include <iostream>
#include "MyString.h"
using namespace std;

int main()
{
	MyString obj1;
	MyString obj2("abcdefg");
	MyString obj3 = obj2;
	MyString obj4 = "abc";
	MyString obj5(obj4);
	MyString obj6(5);

	//obj1 = obj5;
	
	//obj1[4];

	/*cout<<obj1<<endl;

	cout<<obj1[4]<<endl;
	obj1[4] = '3';
	cout<<obj1[4]<<endl;*/

/*	if (obj2 == "abcdefg123")
	{
		cout<<"equal"<<endl;
	}
	else
	{
		cout<<"not equal"<<endl;
	}
	if (obj3 == obj4)
	{
		cout<<"equal"<<endl;
	}
	else
	{
		cout<<"not equal"<<endl;
	}

	if (obj2 > "defg")
	{
		cout<<">"<<endl;
	}
	else
	{
		cout<<"<"<<endl;
	}
	if (obj2 > obj4)
	{
		cout<<">"<<endl;
	}
	else
	{
		cout<<"<"<<endl;
	}*/
	cin>>obj6;

	cout<<obj6<<endl;

	system("pause");
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值