您的位置:首页 > 其它

重载课堂作业

2015-05-06 17:28 190 查看
#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
class String
{
private:
char *s;
public:
friend string operator + ( String &s1, String &s2);
friend bool operator == ( String &s1, String &s2);
friend bool operator > ( String &s1, String &s2);
friend bool operator < ( String &s1, String &s2);
friend ostream & operator << (ostream&,String);
friend istream & operator >> (istream&,String);
void display();
};
void String::display()
{
cout <<s;
}
bool operator < (String &string1,String &string2) //<的重载
{
if(strcmp(string1.s,string2.s)<0)
return OK;
else
return ERROR;
}
bool operator > (String &string1,String &string2)   //>的重载
{
if(strcmp(string1.s,string2.s)>0)
return OK;
else
return ERROR;
}
bool operator == (String &string1,String &string2)  //==的重载
{
if(strcmp(string1.s,string2.s)==0)
return OK;
else
return ERROR;
}
string operator + ( String &s1, String &s2)//+的重载
{
cin>>s1>>s2;
string c=s1+s2;
cout<<c;
}
ostream & operator <<(ostream & output,String &s)  //<<
{
return output;
}
istream & operator >>(istream & input,String &s)   //>>
{
char m[100];
cout << "Please input a string:"<<endl;
input >> m;
}

void compare(String &string1,String &string2)
{
if(operator > (string1,string2)==1)//调用>
{
string1.display();
cout << ">";
string2.display();
}
else
if(operator < (string1,string2)==1)//调用<
{
string1.display();
cout << ">";
string2.display();
}
else
if(operator == (string1,string2)==1)//调用==
{
string1.display();
cout << "=";
string2.display();
}
cout << endl;
}
int main()
{
String string1,string2;
cin>>string1;
cin>>string2;
operator + ( string1, string2);
compare(string1,string2);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: