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.***********************/







原创粉丝点击