C++重载运算符
来源:互联网 发布:最全双色球历史数据库 编辑:程序博客网 时间:2024/06/05 09:40
C++重载运算符
#include <iostream> //重载运算符作业
using namespace std;
#include <cstring>
#include<windows.h>
class String
{
public:
String(){p=NULL;}
String(const String &);
String(char *str);
~String();
String & operator=(const String &);
String operator+(const String &);
friend bool operator>(String &string1,String &string2);
friend bool operator< (String &string1, String &string2);
friend bool operator== (String &string1, String &string2);
friend bool operator!= (String &string1, String &string2);
friend ostream& operator << (ostream&,const String&);
friend istream& operator >> (istream&,String&);
void display();
private:
char *p;
};
String::String(const String &s)
{
if(s.p)
p=new char[strlen(s.p)+1];
else p=0;
strcpy(p,s.p);
}
String::String(char *str)
{
p=str;
}
String::~String()
{
if(p!=NULL)
delete p;
p=NULL;
}
String& String::operator=(const String &s)
{
if(p)
{delete[]p; p=new char[strlen(s.p)+1];}
else
p=new char[strlen(s.p)+1];
strcpy(p,s.p);
return *this;
}
String String::operator +(const String &s)
{
String temp;
if(temp.p!=NULL)
delete[]temp.p;
temp.p = new char[strlen(p)+strlen(s.p)+1];
strcpy(temp.p,p);
strcat(temp.p,s.p);
return temp;
}
bool operator>(String &string1,String &string2)
{
if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator<(String &string1,String &string2)
{
if(!operator>(string1,string2))
return true;
else
return false;
}
bool operator==(String &string1,String &string2)
{
if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
bool operator !=(String &string1, String &string2)
{
if(!(string1.p==string2.p))
return true;
else
return false;
}
ostream& operator <<(ostream &output,const String &s)
{
output<<s.p;
return output;
}
istream& operator >>(istream &intput,String &s)
{
char temp[100000] = {"",};
intput>>temp;
s.p=temp;
return intput;
}
void String::display()
{
cout<<p<<endl;
}
void compare(String &string1,String &string2)
{if(operator>(string1,string2)==1)
{ cout<<"string1>string2"<<endl;}
else
if(operator<(string1,string2)==1)
{
cout<<"string1<string2"<<endl;
}
if(operator==(string1,string2)==1)
{
cout<<"string1=string2<<endl";
}
if(operator==(string1,string2)!=1)
{
cout<<"string1!=string2"<<endl;
}
}
int main()
{
String string1("happy"),string2("birthday");
cout<<"输出string1:";
string1.display();
cout<<"输出string2:";
string2.display();
cout<<"比较string1和string2:";
compare(string1,string2);
String string4,string5,string6;
cout<<"string1和string2连接后赋值给string4:"<<endl;
string4=string1+string2;
string4.display();
cout<<"输入string5:";
cin>>string5;
cout<<"输出string5:"<<endl;
cout<<string5<<endl;
cout<<"string1赋值给string6:"<<endl;
string6=string1;
string6.display();
system("pause");
return 0;
}
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- C#:运算符重载
- [C++]运算符重载
- 【c++】运算符重载
- C++:运算符重载
- C#:运算符重载
- C++:运算符重载
- Python模块安装问题
- nginx提高负载
- 在tableau server 上自定义报表
- Spark First Sample Demo
- iOS-NSDate 相差 8 小时
- C++重载运算符
- Android Studio中的Button英文文本全部显示为大写的解决方法
- poj 1014 dividing (母函数)
- android 版本号 对应的版本名称
- jq ajax 传递数据
- test命令
- matlab bwlabel
- WEB前端解决浏览器兼容性问题
- nyist 737 石子合并(一)(区间dp)