重载课堂作业
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; }
相关文章推荐
- 第四章:运算符的重载的一系列问题(课堂作业)
- 第四章 重载课堂作业
- 第四次C程序课堂作业
- 第六课堂课后作业
- 课堂作业07——MVC概述
- c++ 课堂作业(1)
- 耿丹CS16-2班课堂测试作业汇总
- 课堂作业1
- 2017-2018-1 20155228 《信息安全系统设计基础》第二周课堂测试与课后作业
- 课堂作业一
- 课堂作业之计算器
- 软件工程课堂作业——寻找“水王”
- java课堂作业(二)
- 课堂作业——找“小水王”
- 【C#基础】【语法03】课堂作业题 01-11
- java第八次作业:课堂上发布的前5张图片(包括匿名对象、单例模式恶汉式、自动生成对象、args[]数组使用、静态关键字)
- 课堂作业(电费)
- 课堂在线Java程序设计作业 求斐波那契数
- 课堂作业
- 30道四则运算题目---课堂作业--软件工程c++