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;

相关推荐
小庞在加油5 分钟前
Apollo源码架构解析---附C++代码设计示例
开发语言·c++·架构·自动驾驶·apollo
专注VB编程开发20年38 分钟前
各版本操作系统对.NET支持情况(250707更新)
开发语言·前端·ide·vscode·.net
我喜欢就喜欢1 小时前
RapidFuzz-CPP:高效字符串相似度计算的C++利器
开发语言·c++
千帐灯无此声1 小时前
Linux 测开:日志分析 + 定位 Bug
linux·c语言·c++·bug
莫彩1 小时前
【Modern C++ Part7】_创建对象时使用()和{}的区别
开发语言·c++
星光54221 小时前
飞算JavaAI:给Java开发装上“智能引擎”的超级助手
java·开发语言
June bug1 小时前
【Python基础】变量、运算与内存管理全解析
开发语言·python·职场和发展·测试
醇醛酸醚酮酯1 小时前
Qt项目锻炼——TODO(五)
开发语言·qt
蹦蹦跳跳真可爱5892 小时前
Python----OpenCV(几何变换--图像平移、图像旋转、放射变换、图像缩放、透视变换)
开发语言·人工智能·python·opencv·计算机视觉
骁的小小站2 小时前
HDLBits刷题笔记和一些拓展知识(十一)
开发语言·经验分享·笔记·其他·fpga开发