欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 锐评 > C++:类中的特殊关键字,运算重载符

C++:类中的特殊关键字,运算重载符

2024/10/24 20:16:07 来源:https://blog.csdn.net/m0_58572142/article/details/142500034  浏览:    关键词:C++:类中的特殊关键字,运算重载符

1.My_string类中重载以下的运算符:

+、[] 、>、<、==、>=、<=、!=、+=、输入输出(>>、<<)

主函数:

#include <iostream>
#include "my_string.h"using namespace std;int main()
{My_string s1("cat");My_string s2(" fash");My_string s3=s1+s2;s3.show();cout<<"索引:"<<s3[5]<<endl;My_string s4("beef");My_string s5(" milk");s4+=s5;s4.show();My_string s6("Abc");My_string s7("abc");cout<<(s6>s7)<<endl;cout<<(s6<s7)<<endl;return 0;}

my_string.h

#ifndef MY_STRING_H
#define MY_STRING_H
#include <iostream>
#include <cstring>using namespace std;
class My_string
{
private:char *ptr;int size;int len;
public://无参构造My_string();//有参构造My_string(const char *src);//析构函数~My_string();My_string operator+(My_string &S);char & operator[](int index);bool operator>(const My_string &S) const;bool operator<(const My_string &S) const;bool operator==(const My_string &S) const;bool operator>=(const My_string &S) const;bool operator<=(const My_string &S) const;bool operator!=(const My_string &S) const;My_string & operator+=(const My_string &S);friend ostream& operator<<(ostream &out, const My_string &S);friend istream& operator>>(istream &in, My_string &S);void show();};#endif // MY_STRING_H

my_string.cpp

#include "my_string.h"//无参构造
My_string::My_string():size(15)
{this->ptr=new char[size];this->ptr[0]='\0';this->len=0;
}
//有参构造
My_string::My_string(const char *src)
{len=0;for(int i=0;src[i]!='\0';i++){len++;}size=len+1;this->ptr=new char[size];for(int i=0;i<len;i++){ptr[i]=src[i];}ptr[len]='\0';
}//析构函数
My_string::~My_string()
{delete []ptr;//cout<<"析构函数"<<this<<endl;
}//重载 +操作符My_string My_string::operator+(My_string &S)
{int newlen=len+S.len;char *newptr=new char[newlen+1];//分配新内存strcpy(newptr,this->ptr);strcat(newptr,S.ptr);My_string temp(newptr);//创建临时对象delete []newptr; // 释放临时内存return temp;
}//重载 [] 操作符
char & My_string::operator[](int index)
{if(index<0 ||index>=len){exit(EXIT_FAILURE);}else{return ptr[index-1];}
}// 重载 > 操作符
bool My_string::operator>(const My_string &S) const
{return strcmp(this->ptr, S.ptr) > 0;
}// 重载 < 操作符
bool My_string::operator<(const My_string &S) const
{return strcmp(this->ptr, S.ptr) < 0;
}// 重载 == 操作符
bool My_string::operator==(const My_string &S) const
{return strcmp(this->ptr, S.ptr) == 0;
}// 重载 >= 操作符
bool My_string::operator>=(const My_string &S) const
{return strcmp(this->ptr, S.ptr) >= 0;
}// 重载 <= 操作符
bool My_string::operator<=(const My_string &S) const
{return strcmp(this->ptr, S.ptr) <= 0;
}// 重载 != 操作符
bool My_string::operator!=(const My_string &S) const
{return strcmp(this->ptr, S.ptr) != 0;
}
// 重载 += 操作符
My_string & My_string::operator+=(const My_string &S)
{int newlen=len+S.len;char *newptr = new char[newlen+1];strcpy(newptr, this->ptr);strcat(newptr, S.ptr);delete[] this->ptr;this->ptr = newptr; // 更新 ptr 指向新内存this->len += newlen; // 更新长度this->size = newlen + 1; // 更新容量return *this;
}ostream& operator<<(ostream &out, const My_string &S)
{out << S.ptr;return out;
}istream& operator>>(istream &in, My_string &S)
{delete[] S.ptr; // 释放已有内存S.ptr = new char[S.size]; // 重新分配内存in >> S.ptr; // 读取字符串S.len = strlen(S.ptr); // 更新长度return in;
}
void My_string::show()
{cout<<"*ptr="<<ptr<<endl;cout<<"size="<<size<<endl;cout<<"len="<<len<<endl;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com