C++读书笔记之重载双目运算符 Cplusplus overload binary operator
来源:互联网 发布:天族女捏脸数据图 编辑:程序博客网 时间:2024/05/16 12:10
Overloading binary operators
You overload a binary unary operator with either a nonstatic member functionthat has one parameter, or a nonmember function that has two parameters. Supposea binary operator@ is called with the statementt @ u, where t is an object of typeT, and u isan object of type U. A nonstatic member function that overloadsthis operator would have the following form:
return_type operator@(T)
A nonmember function that overloads the same operator wouldhave the following form:
return_type operator@(T, U)
An overloaded binary operator may return any type.
The following example overloads the * operator:
struct X { // member binary operator void operator*(int) { }};// non-member binary operatorvoid operator*(X, float) { }int main() { X x; int y = 10; float z = 10; x * y; x * z;}
The call x * y is interpreted asx.operator*(y).The callx * z is interpreted asoperator*(x, z).
example1:
#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( ) //输出p所指向的字符串{cout<<p;}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(strcmp(string1.p,string2.p)<0) return true; else return false;}bool operator==(String &string1,String &string2){ if(strcmp(string1.p,string2.p)==0) return true; else return 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("cplusplus"),string2("socket"),string3("linux"),string4("OS"); cout<<"string1>string2 ? 1:yes|0:no -->"<<(string1>string2)<<endl; cout<<"string1<string2 ? 1:yes|0:no -->"<<(string1<string2)<<endl; cout<<"string1==string2 ? 1:yes|0:no -->"<<(string1==string2)<<endl; cout<<"operator>(string1,string2) "<<operator>(string1,string2)<<endl; cout<<"operator<(string1,string2) "<<operator<(string1,string2)<<endl; cout<<"operator==(string1,string2) "<<operator==(string1,string2)<<endl; compare(string1,string2); compare(string2,string3); compare(string1,string4); return 0;}/********************************************运行结果:string1>string2 ? 1:yes|0:no -->0string1<string2 ? 1:yes|0:no -->1string1==string2 ? 1:yes|0:no -->0operator>(string1,string2) 0operator<(string1,string2) 1operator==(string1,string2) 0cplusplus<socketsocket>linuxcplusplus>OSProcess returned 0 (0x0) execution time : 0.125 sPress any key to continue.*********************************************/
example2:
To write a program to add two complex numbers using binary operator overloading
#include<iostream>using namespace std;class complex{ double a,b; public: void getvalue() { cout<<"Enter the value of Complex Numbers a,b:\n"; cin>>a>>b; } complex operator+(complex ob) { complex t; t.a=a+ob.a; t.b=b+ob.b; return(t); } complex operator-(complex ob) { complex t; t.a=a-ob.a; t.b=b-ob.b; return(t); } void display() { if(b>=0) cout<<a<<"+"<<b<<"i"<<"\n"; else cout<<a<<""<<b<<"i"<<"\n"; }};int main(){ complex a,b,c; a.getvalue(); a.display(); b.getvalue(); b.display(); c=a-b; c.display();}/**********************运行结果:Enter the value of Complex Numbers a,b:20136.32013+6.3iEnter the value of Complex Numbers a,b:199111.51991+11.5i22-5.2iProcess returned 0 (0x0) execution time : 12.698 sPress any key to continue.***********************/
- C++读书笔记之重载双目运算符 Cplusplus overload binary operator
- C++读书笔记之重载赋值运算符 overload assignment operator
- Kotlin-31.操作符/运算符重载(operator overload)
- 运算符重载之operator
- C++之双目运算符重载
- 双目运算符重载
- 重载双目运算符
- 双目运算符重载
- 双目运算符重载
- 2.c++-重载运算符operator
- c++之operator运算符重载
- C++读书笔记之 关系运算符重载 大于号> 小于号< 等于号== 重载 overload
- operator重载运算符
- operator 运算符重载
- operator运算符重载
- operator重载运算符
- 双目运算符的重载
- 双目运算符的重载
- Uva 10557
- 不要用充实的借口去浪费时间
- Android开发(10)使用SQLiteDatabase操作SQLite数据库实现增删改查(源码)
- 《学习OpenCV》练习题第三章第二题
- C:表达式、语句、声明
- C++读书笔记之重载双目运算符 Cplusplus overload binary operator
- 新手教程:建立网站的全套流程与详细解释
- 【Android 开发】:UI控件之 ImageView 从网络上获取图像
- Event Validation Errors and Network Congestion in ASP.NET
- 《学习OpenCV》练习题第二章第五题
- MySQL 5.5 权限导出脚本
- AndroidQuery 开源项目
- 来自各移动平台的设计指导方针
- 一个关于兑换零钱的豆瓣笔试题