由重载函数学习c++ (2)
来源:互联网 发布:dr.com mac 编辑:程序博客网 时间:2024/06/18 03:55
#include <iostream>#include <cstring>using namespace std;class String{public:String(){p=NULL;}String( char *str);friend bool operator >(String &string1,String &string2);friend bool operator <(String &string1,String &string2);friend bool operator ==(String &string1,String &string2);void display();private: char *p;};String::String( char *str){p=str;}void String::display(){cout<<p;//输出p所指向的字符串}bool operator>(String &string1,String &string2){if(strcmp(string1.p,string2.p)>0){return true;}elsereturn false;}bool operator<(String &string1,String &string2){if(strcmp(string1.p,string2.p)<0){return true;}elsereturn false;}bool operator==(String &string1,String &string2){if(strcmp(string1.p,string2.p)==0){return true;}elsereturn false;}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("Hello"),string2("Book"),string3("computer"),string4("Hello");compare(string1,string2);compare(string2,string3);compare(string1,string4);return 0;
}
ubuntu@ubuntu:~$ g++ 13.cpp -o a13.cpp: In function ‘int main()’:13.cpp:79:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] String string1("Hello"),string2("Book"),string3("computer"),string4("Hello"); ^13.cpp:79:40: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] String string1("Hello"),string2("Book"),string3("computer"),string4("Hello"); ^13.cpp:79:60: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] String string1("Hello"),string2("Book"),string3("computer"),string4("Hello"); ^13.cpp:79:77: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings] String string1("Hello"),string2("Book"),string3("computer"),string4("Hello"); ^ubuntu@ubuntu:~$ ./aHello>BookBook<computerHello==Hello
要注意这个程序中,需用到头文件#include <cstring>,有关这个头文件的使用,目前我也不是太清楚!对于运行的警告信息,暂时无法作出解释!
0 0
- 由重载函数学习c++ (2)
- 由重载学习写C++(1)
- 【C++】【学习笔记】【006】函数的重载
- C++primer学习笔记-----6.4函数重载
- (C/C++学习笔记)函数模板遇上函数重载
- C++:重载函数2(计算面积)
- C/C++ 函数重载
- [c++]函数重载
- C++:函数重载
- Objective-C函数重载
- c++-函数重载
- C++-函数重载
- C++_函数重载
- 【C++】函数的重载
- 【C++】:C++函数重载
- [C++]函数重载
- C/C++函数重载
- <C++>_函数重载
- Android 基础教程之---动态更改屏幕方向的简单例子(LANDSCAPE与PORTRAIT)!
- 用来减少SQL的查询类库
- 工作两年后,我们需要重新思考生活,工作和未来的方向
- 5-36 复数四则运算
- 【Heaven Cow与God Bull】题解
- 由重载函数学习c++ (2)
- 关于Android中so的符号表导出以及C++的符号改编规则
- java实现DES数据加密与解密
- need improve 43. Multiply Strings
- 邮件协议学习
- public private protected
- hdu1029 Ignatius and the Princess IV
- 数组简单实现单向链表
- mysql中文乱码解决