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;

相关推荐
m0_7482402542 分钟前
Windows编程+使用C++编写EXE加壳程序
开发语言·c++·windows
兮兮能吃能睡2 小时前
R语言模型分析(一)(1)
开发语言·r语言
LoveXming2 小时前
Chapter14—中介者模式
c++·microsoft·设计模式·中介者模式·开闭原则
wuk9984 小时前
基于有限差分法的二维平面热传导模型MATLAB实现
开发语言·matlab·平面
杨筱毅5 小时前
【C++】【常见面试题】最简版带大小和超时限制的LRU缓存实现
c++·面试
初见无风5 小时前
2.5 Lua代码中string类型常用API
开发语言·lua·lua5.4
做运维的阿瑞6 小时前
用 Python 构建稳健的数据分析流水线
开发语言·python·数据分析
左师佑图6 小时前
综合案例:Python 数据处理——从Excel文件到数据分析
开发语言·python·数据分析·excel·pandas
陌路206 小时前
C23构造函数与析构函数
开发语言·c++
_OP_CHEN7 小时前
C++进阶:(二)多态的深度解析
开发语言·c++·多态·抽象类·虚函数·多态的底层原理·多态面试题