#include <bits/stdc++.h>
using namespace std;
class String{
protected:
char *s;
public:
String(){s=NULL;}//定义默认构造函数
String(char *str);//声明构造函数
String(const String &stri);//拷贝构造函数
~String();//析构函数
String operator=(const String &str);//成员函数重载=
String operator+=(String &str);//成员函数重载+=
String operator+(String &str1);//成员函数重载+
friend bool operator>(String &str1,String &str2);//友元函数重载>
friend bool operator>=(String &str1,String &str2);//友元函数重载>=
friend bool operator<(String &str1,String &str2);//友元函数重载<
friend bool operator<=(String &str1,String &str2);//友元函数重载<=
friend bool operator==(String &str1,String &str2);//友元函数重载==
friend bool operator!=(String &str1,String &str2);//友元函数重载!=
void display();
};
char* Stract(char *str1,char*str2)//自定义字符串连接函数
{
char *str;
int i,j;
int len1 = strlen(str1);
int len2 = strlen(str2);
int len = len1+len2;
str = new char(len+1);
strcpy(str,str1);
for(i=0,j=len1;i<len2+1;i++,j++){
str[j]=str2[i];
}
return str;
}
String::String(char *str)
{
int len=strlen(str);
s = new char(len+1);
strcpy(s,str);
}
String::String(const String &stri)
{
int len=strlen(stri.s);
s = new char(len+1);
strcpy(s,stri.s);
}
String::~String()
{
delete []s;
}
String String::operator=(const String &str)
{
int len=strlen(str.s);
s = new char(len+1);
strcpy(s,str.s);
return *this;
}
String String::operator+=(String &str)
{
int len=strlen(s);
char *p;
p = new char(len+1);
strcpy(p,s);
delete []s;
len+=strlen(str.s);
s =new char(len+1);
strcpy(s,Stract(p,str.s));
delete []p;
return *this;
}
String String::operator+(String &str1)
{
String str;
int len = strlen(s)+strlen(str1.s);
str.s = new char(len+1);
char *pc = Stract(s,str1.s);
strcpy(str.s,pc);
delete[] pc;
return str;
}
bool operator>(String &str1,String &str2)
{
if(strcmp(str1.s,str2.s)>0) return true;
else{
return false;
}
}
bool operator>=(String &str1,String &str2)
{
if(strcmp(str1.s,str2.s)>=0) return true;
else{
return false;
}
}
bool operator<(String &str1,String &str2)
{
if(strcmp(str1.s,str2.s)<0) return true;
else{
return false;
}
}
bool operator<=(String &str1,String &str2)
{
if(strcmp(str1.s,str2.s)<=0) return true;
else{
return false;
}
}
bool operator==(String &str1,String &str2)
{
if(strcmp(str1.s,str2.s)==0) return true;
else{
return false;
}
}
bool operator!=(String &str1,String &str2)
{
if(strcmp(str1.s,str2.s)!=0) return true;
else{
return false;
}
}
void String::display()
{
cout<<s<<endl;
}
int main()
{
String a((char*)"Hello");
String b((char*)"Word");
String c((char*)"wu");
String d((char*)"mao");
String e((char*)"lin");
String f((char*)"xyz");
String g((char*)"xyz");
cout<<"a=Hello"<<' '<<"b=Word"<<' '<<"c=wu"<<' '<<"d=mao"<<' '<<"e=lin"<<' '<<"f=xyz"<<' '<<"f=xyz"<<endl;
cout<<"赋值 =:"<<endl;
c=a;
cout<<"c = a =";
c.display();
cout<<"加法 +:"<<endl;
c=(a+b);
cout<<"a + b =";
c.display();
cout<<"+=: "<<endl;
a+=b;
cout<<"a += b =";
a.display();
cout<<"/************************/"<<endl;
if(d>e) cout<<"d>e"<<endl;
if(f>=e) cout<<"f>=e"<<endl;
if(b<e) cout<<"b<e"<<endl;
if(d<=f) cout<<"d<=f"<<endl;
if(f==g) cout<<"b==e"<<endl;
if(d!=f) cout<<"d!=f"<<endl;
return 0;
}
c++第四次实验:运算符重载
最新推荐文章于 2021-12-19 13:50:43 发布
这篇博客介绍了如何创建一个自定义的String类,并实现了构造函数、拷贝构造函数、析构函数以及各种操作符(=、+=、+、>、>=、<、<=、==、!=)的重载。通过这个类,可以进行字符串的比较和拼接等操作。示例中展示了如何使用这些功能进行字符串的赋值、加法和比较。
摘要由CSDN通过智能技术生成