9月3日 C++

include <iostream>

include <cstring>

sing namespace std;

lass Stu

public:

//无参构造函数

Stu()

{

a=nullptr;

len=0;

}

//有参构造函数

Stu(const char *s)

{

if(s!=nullptr)

{

len=strlen(s);

a=new char[len+1];

strcpy(a,s);

}

else

{

a=nullptr;

len=0;

}

}

//析构函数

~Stu()

{

delete []a;

a=nullptr;

}

//拷贝构造函数

Stu (const Stu& other)

{

len=other.len;

a=new char[len+1];

strcpy(a,other.a);

}

//拷贝赋值函数

Stu& operator=(const Stu& other)

{

if(&other!=this)

{

len=other.len;

if(a==nullptr)

{

a=new char[len+1];

}

strcpy(a,other.a);

}

return *this;

}

const char *data()

{

if(a!=nullptr)

{

char *s=nullptr;

s=a;

return s;

}

else

{

cout << "空字符串无法展示" << endl;

return nullptr;

}

}

//连接字符串

const Stu operator+(Stu &R) const

{

Stu x;

if(a!=nullptr&&R.a!=nullptr)

{

x.len=len+R.len;

x.a=new char[len+1];

strcpy(x.a,a);

strcat(x.a,R.a);

return x;

}

else

{

cout << "空字符串无法拼接" << endl;

return x;

}

}

//不等于

const bool operator!=(Stu &R)const

{

if((a!=nullptr)&&(R.a!=nullptr))

{

if(strcmp(a,R.a)==0)

return 0;

else

return 1;

}

else

{

cout << "字符串不存在" << endl;

return 0;

}

}

//等于

const bool operator==(Stu &R)const

{

if(strcmp(a,R.a)==0)

return 1;

return 0;

}

//小于

const bool operator<(Stu &R)const

{

int x=strcmp(a,R.a);

if(x<0)

return 1;

else

return 0;

}

//小于等于

const bool operator<=(Stu &R)const

{

int x=strcmp(a,R.a);

if(x<0||x==0)

return 1;

return 0;

}

//大于

const bool operator>(Stu &R)const

{

int x=strcmp(a,R.a);

if(x>0)

return 1;

return 0;

}

//大于等于

const bool operator>=(Stu &R)const

{

int x=strcmp(a,R.a);

if(x>0||x==0)

return 1;

return 0;

}

//size

const int size()const

{

return strlen(a);

}

friend ostream& operator<<(ostream&L,const Stu&R);

friend istream& operator>>(istream& L, Stu& R);

private:

char *a;

int len;

;

stream& operator<<(ostream& L, const Stu&R)

{

L <<"字符串="<< R.a <<"长度=" << R.size() <<endl;

return L;

}

istream& operator>>(istream& L, Stu& R)

{

cout<<"请输入\n";

L >>R.a;

return L;

}

nt main(int argc, const char *argv[])

Stu s1("hello");

Stu s2("hell");

Stu s3;

s3=s1+s2;

cout << "s3=" << s3.data() << endl;

bool ret=s1!=s2;

cout << "s1!=s2 " << ret << endl;

ret=s1==s2;

cout << "s1==s2 " << ret << endl;

ret=s1<s2;

cout << "s1<s2 " << ret << endl;

ret=s1<=s2;

cout << "s1<=s2 " << ret << endl;

ret=s1>s2;

cout << "s1>s2 " << ret << endl;

ret=s1>=s2;

cout << "s1>=s2 " << ret << endl;

cin>>s3;

cout <<s3;

return 0;

相关推荐
拾忆,想起1 小时前
Dubbo超时问题排查与调优指南:从根因到解决方案
服务器·开发语言·网络·微服务·架构·php·dubbo
7ioik2 小时前
什么是线程池?线程池的作用?线程池的四种创建方法?
java·开发语言·spring
晨非辰2 小时前
数据结构排序系列指南:从O(n²)到O(n),计数排序如何实现线性时间复杂度
运维·数据结构·c++·人工智能·后端·深度学习·排序算法
寻星探路2 小时前
JavaSE重点总结后篇
java·开发语言·算法
Charles_go3 小时前
C#中级8、什么是缓存
开发语言·缓存·c#
残影飞雪3 小时前
Jetson版本下Pytorch和torchvision
c++
松涛和鸣4 小时前
14、C 语言进阶:函数指针、typedef、二级指针、const 指针
c语言·开发语言·算法·排序算法·学习方法
智商低情商凑8 小时前
Go学习之 - Goroutines和channels
开发语言·学习·golang
半桶水专家8 小时前
Go 语言时间处理(time 包)详解
开发语言·后端·golang
编程点滴8 小时前
Go 重试机制终极指南:基于 go-retry 打造可靠容错系统
开发语言·后端·golang