基本运算符重载实例
来源:互联网 发布:中国社交网络发展历程 编辑:程序博客网 时间:2024/05/22 11:40
Mystring.h
#include <iostream>#ifndef __MYSTRING_H#define __MYSTRING_Hclass Mystring{friend void print(Mystring & oop);public:/*空串 “”*/Mystring();Mystring(char *str);Mystring(Mystring &oop);~Mystring();Mystring& operator=(Mystring &oop);public:/*[]操作符重载*/char& operator[](int index);public:/*==,!=操作符重载*/bool operator==(Mystring &oop);bool operator==(const Mystring &oop);bool operator!=(const Mystring &oop);bool operator!=(Mystring &oop);public:/*括号操作符重载*/char* Mystring::operator()(const char* str);private:int str_len;char *str;};void print(Mystring & oop);#endifMystring.cpp
#define _CRT_SECURE_NO_WARNINGS#include "Mystring.h"#include <iostream>#include <cstring>Mystring::Mystring(){str_len = 0;this->str = new char[this->str_len + 1];strcpy(this->str, "");}Mystring::Mystring(char *str){if (str == NULL){str_len = 0;this->str = new char[this->str_len + 1];strcpy(this->str, "");}else{this->str_len = strlen(str);this->str = new char[str_len+1];strcpy(this->str,str);}}/*拷贝构造函数*/Mystring::Mystring(Mystring & oop){this->str_len = oop.str_len;this->str = new char[str_len + 1];strcpy(this->str,oop.str);}/*析构函数*/Mystring::~Mystring(){if (this->str != NULL){delete this->str;this->str = NULL;this->str_len = 0;std::cout << "析构函数生效" << std::endl;}}/*输出字符串信息*/void print(Mystring & oop){std::cout << oop.str << std::endl;}/*等号操作符重载*/Mystring & Mystring::operator=(Mystring &oop){this->str_len = oop.str_len;this->str = new char[str_len + 1];strcpy(this->str, oop.str);return *this;}/*[]操作符重载*/char& Mystring::operator[](int index){return this->str[index];}/*重载 == ,!=等操作*/bool Mystring::operator==(Mystring &oop){if(this->str_len != oop.str_len) return false;else{if (this->str_len == oop.str_len){for (int i = 0; i < oop.str_len; i++){if (this->str[i] != oop.str[i])return false;continue;}return true;}}}bool Mystring::operator==(const Mystring &oop){if (this->str_len != oop.str_len)return false;else{if (this->str_len == oop.str_len){for (int i = 0; i < oop.str_len; i++){if (this->str[i] != oop.str[i])return false;continue;}return true;}}}bool Mystring::operator!=(const Mystring &oop){if (this->str_len != oop.str_len)return true;else{if (this->str_len == oop.str_len){for (int i = 0; i < oop.str_len; i++){if (this->str[i] == oop.str[i])continue;elsereturn true;}return false;}}}bool Mystring::operator!=(Mystring &oop){if (this->str_len != oop.str_len)return true;else{if (this->str_len == oop.str_len){for (int i = 0; i < oop.str_len; i++){if (this->str[i] == oop.str[i])continue;elsereturn true;}return false;}}}/*括号操作符重载*/char* Mystring::operator()(const char* str){this->str_len = strlen(str);this->str = new char[this->str_len + 1];strcpy(this->str,str);return this->str;}
main.cpp验证:
#include <iostream>#include "Mystring.h"#include <cstring>using namespace std;class mfghj{private:int sl;int bl;};int main(){{Mystring oop1("baibai");const Mystring oop2("nihao");Mystring oop3;char* re = oop3("zaiganma");/*括号运算符验证*/cout<<re<<endl;#if 0Mystring oop3 = oop2;//调用构造函数实现print(oop3);oop2 = oop1;//“等号作符验证 字符串复制操作,通过等号运算符重载实现”print(oop2);cout << oop2[0] << endl;//[]操作符重载验证#endifcout << "判断两个对象:" << (oop1 == oop2) << endl;/*验证==*/cout << "判断两个对象是否不想等" << (oop1 != oop2) << endl;}/*string str1 = "nihao";string str2 = "nihao";if (str1 == str2){cout << "相等" << endl;}elsecout << "不想等" << endl;*/system("pause");return 0;}
阅读全文
0 0
- 基本运算符重载实例
- 运算符重载实例
- 运算符重载实例
- 运算符重载实例
- C# 基本运算符重载
- python运算符重载实例
- C++运算符重载实例
- 运算符重载实例:数组
- c++运算符重载+实例
- C# 运算符重载实例
- c++运算符重载实例
- c++运算符重载实例
- C++中运算符重载实例
- C++编程实例-运算符重载
- ADA程序实例(运算符重载)
- 运算符重载分析与实例
- C++实例——运算符重载
- 第十六讲:运算符重载 (补充实例)
- faster rcnn 与ssd的架构
- Java中HashMap和TreeMap的区别
- linux系统Samba安装配置实现与Windows文件共享
- Android Studio打包
- java运算符优先级
- 基本运算符重载实例
- 使用kubeadm将Kubernetes集群从1.6版本升级到1.7
- 爬虫maizi
- log4j有针对性的将日志输出到不同文件且不重复输出到root中
- Centos Yum error – ImportError: No module named cElementTree
- Spring框架中的各种*Aware接口
- PostMessage和SendMessage
- 前端JS如何获取主域名(根域名)
- JavaScript中的call和apply用法