C++学习笔记:MyString类的重载 练习

首先测试.cpp

// MyString类案例.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include"MyString.h"
#pragma warning(disable:4996)

int _tmain(int argc, _TCHAR* argv[])
{
	MyString str1("lifeng");//MyString str1(const char* p);
	MyString str2(str1);//MyString str2(MyString &str1);
	MyString str3 = str1;
	MyString str4 = "nihao";
	str4[0] = 'w';//char& operator[](int index);
	if (str1 == str4){//bool operator==(MyString &str4);
		cout << "两者相等" << endl;
	}
	else if(str1!=str4){//bool operator!=(MySring &str);
		cout << "两者不等" << endl;
	}
	if (str1 < str4){//bool operator<(MyString &str);
		cout << "小于" << endl;
	}
	else if (str1>str4){//bool operator>(MyString &str);
		cout << "大于" << endl;
	}
	MyString s5 = "abcd";
	//strcpy((LPCTSTR)s5.m_p, "ad");//MFC
	strcpy((char*)s5.c_str(), "adc");
	MyString s(12);
	cin >> s;//isstream &operator>>(istream &cin,MyString &s);
	cout << s << endl;
	return 0;
}
头文件,MyString.h

#include"stdafx.h"
#include <iostream>

using namespace std;
class MyString{
public:
	MyString(int len=0);
	MyString(const char*p);
	MyString(MyString &str);
	char& operator[](int index);
	bool operator==(MyString &str);
	bool operator!=(MyString &str);
	int operator<(MyString &str) const;
	int operator<(const char*p) const;
	int operator>(MyString &str) const;
	int operator>(const char *p) const;
	//把类的指针属性 露出来
	const char *c_str();
	friend istream &operator>>(istream &in, MyString &s);//全局函数
	friend ostream &operator<<(ostream &out, MyString &s);
private:
	int m_len;
	char *m_p;
};

MyString.cpp

#include"stdafx.h"
#include"MyString.h"
#pragma warning(disable:4996)

istream &operator>>(istream &in, MyString &s){
	cin >> s.m_p;
	return in;
}
ostream &operator<<(ostream &out, MyString &s){
	cout<<s.m_p;
	return out;
}
MyString::MyString(int len)
{
	if (len == 0){
		m_len = 0;
		m_p = new char[1];
		strcpy(m_p, "");
	}
	else
	{
		m_len = len;
		m_p = new char[m_len + 1];
		memset(m_p, 0, m_len);
	}
}
MyString::MyString(const char*p){
	if (m_p != NULL){
		delete[]m_p;
		m_p = NULL;
		m_len = 0;
	}
	m_len = strlen(p);
	if (m_len == 0){
		m_p = new char[1];
		strcpy(m_p, "");
	}
	else{
		m_p = new char[m_len + 1];
		strcpy(m_p, p);
	}
}
MyString::MyString(MyString &str){
	if (m_p != NULL){
		delete[]m_p;
		m_p = NULL;
		m_len = 0;
	}
	m_len = str.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p,str.m_p);
}
char& MyString::operator[](int index){
	return m_p[index];
}
bool MyString::operator==(MyString &str){
	if (str.m_len==m_len){
		for (int i = 0; i < m_len; i++){
			if (str[i] != m_p[i]){
				return false;
			}
		}
		return true;
	}
	else{
		return false;
	}
}
bool MyString::operator!=(MyString &str){
	return !(*this == str);
}
int MyString::operator<(MyString &str) const{
	return strcmp(m_p, str.m_p);
}
int MyString::operator<(const char*p) const{
	return strcmp(this->m_p,p);
}
int MyString::operator>(MyString &str) const{
	return strcmp(this->m_p,str.m_p);
}
int MyString::operator>(const char *p) const{
	return strcmp(this->m_p, p);
}
const char *MyString::c_str(){
	return m_p;
} 

运行结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值