博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
运算符重载之数组实例的应用
阅读量:4211 次
发布时间:2019-05-26

本文共 4428 字,大约阅读时间需要 14 分钟。

关于运算符重载的各种应用,具体看代码注释

 

MyString.h

#pragma once#include
using namespace std;//c中没有字符串,字符串类(c风格的字符串)//空串 ""class MyString{ friend ostream& operator<<(ostream &out,MyString &s); //这里是函数运算符重载,之所以用全局而不是用成员函数,是因为第一个操作数无法通过this指针隐士传递,第一个操作数其类型是ostream friend istream& operator>>(istream& in,MyString &s);public: MyString(int len = 0); MyString(const char* p); MyString(const MyString& s); ~MyString();public: //重载=号运算符 MyString& operator=(const char* p); //第一个操作数通过this指针隐式传递 MyString& operator=(const MyString& s); char& operator[](int index);public: //重载 == !== bool operator==(const char *p) const; bool operator==(const MyString &s) const; bool operator!=(const char *p) const; bool operator!=(const MyString& s) const;public: int operator<(const char *p); int operator>(const char *p); int operator<(const MyString &s); int operator>(const MyString& s); //把类的指针露出来public: char* c_str() { return m_p; } const char* c_str2() { return m_p; } int length() { return m_len; }private: int m_len; char* m_p;};

MyString.cpp

#define _CRT_SECURE_NO_WARNINGS#include "MyString.h"ostream& operator<<(ostream& out, MyString& s){	out << s.m_p;	return out;}istream& operator>>(istream& in, MyString& s){	cin >> s.m_p;	return in;}MyString::MyString(int len){	if (len == 0)	{		m_len = 0;		m_p = new char[m_len+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 (p == NULL)	{		m_len = 0;		m_p = new char[m_len + 1];		strcpy(m_p, "");	}	else	{		m_len = strlen(p);		m_p = new char[m_len + 1];		strcpy(m_p,p);	}}//拷贝构造函数MyString::MyString(const MyString& s){	m_len = s.m_len;	m_p = new char[m_len+1];	strcpy(m_p,s.m_p);}MyString::~MyString(){	if(m_p!=NULL)	{		delete[] m_p;		m_p = NULL;		m_len = 0;	}} s4="s2222"MyString& MyString::operator=(const char* p){	//旧内存释放掉	if (m_p != NULL)	{		delete[] m_p;		m_p = NULL;		m_len = 0;	}	if (p == NULL)	{		m_len = 0;		m_p = new char[m_len+1];		strcpy(m_p,"");	}	else	{		m_len = strlen(p);		m_p = new char[m_len + 1];		strcpy(m_p,p);	}	return *this;}//s4=s2MyString& MyString::operator=(const MyString& s){	//把旧的内存释放掉	if (m_p != NULL)	{		delete[] m_p;		m_p = NULL;		m_len = 0;	}	if (s == NULL)	{		m_len = 0;		m_p = new char[m_len + 1];		strcpy(m_p,"");	}	else	{		m_len = s.m_len;		m_p = new char[m_len + 1];		strcpy(m_p, s.m_p);	}	return *this;} char& MyString::operator[](int index)  //注意这里,第一个操作数通过this指针隐式传递,{	return m_p[index];}//if (s2=="s2222222")bool MyString::operator==(const char* p) const{	if (p == NULL)	{		if (m_len == 0)		{			return true;		}		else		{			return false;		}	}	else	{		if (m_len == strlen(p))		{			return !strcmp(m_p,p);		}		else		{			return false;		}	}}bool MyString::operator!=(const char* p) const{	return !(*this == p);}bool MyString::operator==(const MyString& s) const{	if (m_len != s.m_len)	{		return false;	}	return !strcmp(m_p,s.m_p);}bool MyString::operator!=(const MyString& s) const{	return !(*this == s);}//if (s3 < "bbbb")int MyString::operator<(const char* p){	return strcmp(this->m_p, p);}int MyString::operator>(const char* p){	return strcmp(p, this->m_p);}int MyString::operator<(const MyString& s){	return strcmp(this->m_p, s.m_p);}int MyString::operator>(const MyString& s){	return  strcmp(s.m_p, m_p);}

测试类:

#define _CRT_SECURE_NO_WARNINGS#include 
using namespace std;#include "MyString.h"void main01(){ MyString s1; MyString s2("s2"); MyString s2_2 = NULL; MyString s3 = s2; MyString s4 = "s4444444444"; //测试运算符重载 和 重载[] //= s4 = s2; s4 = "s2222"; s4[1] = '4'; printf("%c", s4[1]); cout << s4 << endl; //ostream& operator<<(ostream &out, MyString &s) //char& operator[] (int index) //MyString& operator=(const char *p); //MyString& operator=(const MyString &s); cout << "hello..." << endl; system("pause"); return;}void main02(){ MyString s1; MyString s2("s2"); MyString s3 = s2; if (s2 == "aa") { printf("相等"); } else { printf("不相等"); } if (s3 == s2) { printf("相等"); } else { printf("不相等"); }}void main03(){ MyString s1; MyString s2("s2"); MyString s3 = s2; s3 = "aaa"; int tag = (s3 < "bbbb"); if (tag < 0) { printf("s3 小于 bbbb"); } else { printf("s3 大于 bbbb"); } MyString s4 = "aaaaffff"; strcpy(s4.c_str(), "aa111"); //MFC cout << s4 << endl;}void main011(){ MyString s1(128); cout << "\n请输入字符串(回车结束)"; cin >> s1; cout << s1; system("pause");}void main(){ MyString s1(128); cout << "\n请输入字符串(回车结束)"; cin >> s1; cout << s1 << endl; system("pause");}

 

转载地址:http://yrzmi.baihongyu.com/

你可能感兴趣的文章
Secure CRT 自动记录日志 配置 小记
查看>>
RMAN RAC 到 单实例 duplicate 自动分配通道 触发 ORA-19505 错误
查看>>
mysql 随机分页的优化
查看>>
SQL SERVER中判断某个字段是否包含大写字母
查看>>
修改master库文件路径
查看>>
拷贝增量文件
查看>>
mysql中的 skip-name-resolve 问题
查看>>
删除最后一次的备份文件vbs
查看>>
vbs 实现压缩文件夹为zip文件
查看>>
在VBS中,SET的用法
查看>>
组策略合理限制VBS文件
查看>>
VBS不支持跨盘操作
查看>>
MySQl备份恢复策略(完全+增量备份策略)
查看>>
VBS发送邮件
查看>>
锁的兼容性
查看>>
解决mysqldump备份报错:SET OPTION SQL_QUOTE_SHOW_CREATE=1
查看>>
mysqldump参数详细说明
查看>>
mysqldump备份及结合binlog日志恢复的全过程
查看>>
SQL Server 查找占用CUP内存的SQL
查看>>
ms sql server缓存清除与内存释放
查看>>