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 charlen+1;

strcpy(a,s);

}

else

{

a=nullptr;

len=0;

}

}

//析构函数

~Stu()

{

delete \[\]a;

a=nullptr;

}

//拷贝构造函数

Stu (const Stu& other)

{

len=other.len;

a=new charlen+1;

strcpy(a,other.a);

}

//拷贝赋值函数

Stu& operator=(const Stu& other)

{

if(&other!=this)

{

len=other.len;

if(a==nullptr)

{

a=new charlen+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 charlen+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;

相关推荐
爱奥尼欧3 分钟前
轻量级可扩展日志框架-异步日志与系统集成
开发语言·数据库·c++·学习
大圣编程6 分钟前
python break语句
开发语言·前端·python
爱奥尼欧9 分钟前
轻量级可扩展日志框架-日志落地与日志器模块实现
jvm·数据库·c++
AI-好学者16 分钟前
MCP企业运用全面知识点-基础篇
服务器·开发语言·网络·人工智能·python·架构
ch.ju19 分钟前
Java程序设计(第3版)第四章——类加载
java·开发语言
河阿里20 分钟前
SLF4J深度指南(Java):从原理到 Spring 项目实战
java·开发语言·spring
小沈同学呀23 分钟前
飞书机器人+Spring AI Function Calling实战-扔掉MCP Client让LLM直接操控工具
java·开发语言·functioncalling·spring ai·飞书机器人
我不是懒洋洋27 分钟前
从零实现一个加密库:AES与RSA
c++
雨师@29 分钟前
go语言项目--实例化(图书管理)--006
开发语言·后端·golang
Rotion_深31 分钟前
C# 值类型与引用类型 详解
开发语言·jvm·c#