C++读书笔记之重载双目运算符 Cplusplus overload binary operator

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 statement t @ u, where t is an object of type T, 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 operator
void 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 call x * 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 -->0
string1<string2 ? 1:yes|0:no -->1
string1==string2 ? 1:yes|0:no -->0
operator>(string1,string2)  0
operator<(string1,string2)  1
operator==(string1,string2)  0
cplusplus<socket
socket>linux
cplusplus>OS

Process returned 0 (0x0)   execution time : 0.125 s
Press 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:
2013
6.3
2013+6.3i
Enter the value of Complex Numbers a,b:
1991
11.5
1991+11.5i
22-5.2i

Process returned 0 (0x0)   execution time : 12.698 s
Press any key to continue.


***********************/







你可能感兴趣的:(C++,读书笔记,cc++)