Operator overloading in C + +

Source: Internet
Author: User

Four types of operators that cannot be overloaded:

1>:: domain operator

2>. Member access operators

3> * Member pointer access operator ((*this). Member)

4>:? Trinocular operator


Operators that can only be overloaded with a tuple:

1> << output operator overloading

2> >> input operator overloading



An operator overload that declares a friend and can be declared as a member function of a class:

1> arithmetic character

2> + +--operator

3> = = = = = *=/= operator


Operators that can only be declared as member functions of a class

1>[] Subscript operator

2> ()

3>

4>= assignment operator


Operators that are generally not overloaded

1>=

2>&

3>*

In addition, overloads can only be existing operators, and the precedence of operators cannot be changed after overloading.


Here is an example:

  complex.h//  train5////  created by student on 15/8/ 11.//  copyright  (c)  2015 year  personals. all rights reserved.//#ifndef  __train5__complex__#define __train5__complex__#include <iostream>using namespace  std;class complex{public:    complex (int r1=0,int i1=0);     ~complex () {}    const complex& operator--();          //Pre-reduction (real part minus one, imaginary part unchanged)     const Complex  operator--(int);        //Reduction         reduction  friend const complex operator+ (const complex& p1,const complex& &NBSP;P2);     friend const complex operator-(const Complex&  P1,CONST&NBSP;COMPLEX&AMP;&NBSP;P2);     friend const complex operator* (Const complex& p1,const &NBSP;COMPLEX&AMP;&NBSP;P2);     friend const complex operator/(const  COMPLEX&AMP;&NBSP;P1,CONST&NBSP;COMPLEX&AMP;&NBSP;P2);    friend complex  operator+= (COMPLEX&AMP;&NBSP;C1,CONST&NBSP;COMPLEX&AMP;&NBSP;C2);     friend complex  operator-= (COMPLEX&AMP;&NBSP;C1,CONST&NBSP;COMPLEX&AMP;&NBSP;C2);    friend  Complex operator*= (COMPLEX&AMP;&NBSP;C1,CONST&NBSP;COMPLEX&AMP;&NBSP;C2);     friend  complex operator/= (COMPLEX&AMP;&NBSP;C1,CONST&NBSP;COMPLEX&AMP;&NBSP;C2);         friend ostream& operator<< (ostream& out,const  COMPLEX&AMP;&NBSP;C1);     friend bool operator== (const Complex& c1 , CONST&NBSP;COMPLEX&AMP;&NBSP;C2); &nbsP;   friend bool operator!= (const complex& c1,const complex& &NBSP;C2);    private:    int r,i;}; #endif  /* defined (__train5__complex__)  */


  complex.cpp//  train5////  created by student on 15/8/ 11.//  copyright  (c)  2015 year  personals. all rights reserved.//#include   "Complex.h" Complex::complex (INT&NBSP;R1,INT&NBSP;I1) {    r=r1;    &NBSP;I=I1;} const complex operator+ (CONST&NBSP;COMPLEX&AMP;&NBSP;P1,CONST&NBSP;COMPLEX&AMP;&NBSP;P2) {     return complex (p1.r+p2.r,p1.i+p2.i);} const complex operator-(CONST&NBSP;COMPLEX&AMP;&NBSP;P1,CONST&NBSP;COMPLEX&AMP;&NBSP;P2) {     return complex (p1.r-p2.r,p1.i-p2.i);} const complex operator* (CONST&NBSP;COMPLEX&AMP;&NBSP;P1,CONST&NBSP;COMPLEX&AMP;&NBSP;P2) {     return complex (P1.R*P2.R-P1.I*P2.I,P1.R*P2.I+P1.I*P2.R);} const complex operator/(CONST&NBSP;COMPLEX&AMP;&NBSP;P1,CONST&NBSP;COMPLEX&AMP;&NBSP;P2) {     float den=p2.r*p2.r+p2.i*p2.i;    return complex ((p1.r*p2.r+p1.i*p2.i)/den, (-P1.R*P2.I+P1.I*P2.R)/den);} complex operator+= (COMPLEX&AMP;&NBSP;C1,CONST&NBSP;COMPLEX&AMP;&NBSP;C2) {&NBSP;&NBSP;&NBSP;&NBSP;C1=C1+C2 ;     return c1;} complex operator-= (COMPLEX&AMP;&NBSP;C1,CONST&NBSP;COMPLEX&AMP;&NBSP;C2) {&NBSP;&NBSP;&NBSP;&NBSP;C1=C1-C2 ;     return c1;} Complex operator*= (COMPLEX&AMP;&NBSP;C1,CONST&NBSP;COMPLEX&AMP;&NBSP;C2) {&NBSP;&NBSP;&NBSP;&NBSP;C1=C1*C2 ;     return c1;} Complex operator/= (COMPLEX&AMP;&NBSP;C1,CONST&NBSP;COMPLEX&AMP;&NBSP;C2) {&NBSP;&NBSP;&NBSP;&NBSP;C1=C1/C2 ;     return c1;} /*const complex& complex::operator--() {    this->r-=1;     return *this;} *//*const complex complex::operator--(int) {    complex old =  (* this);     --(*this);    return old;} */const complex& complex::operator--() {    complex c (1,0);      (*this) = (*this)-c;    return *this;} const complex complex::operator--(int) {    complex old =  (*this);     complex c (1,0);     (*this) = (*this)-c;     Return old;} ostream& operator<< (OSTREAM&AMP;&NBSP;OUT,CONST&NBSP;COMPLEX&AMP;&NBSP;C1) {     out<< "r=" <<c1.r<< "i=" <<c1.i<<endl;    return out ;} bool operator== (CONST&NBSP;COMPLEX&AMP;&NBSP;C1,CONST&NBSP;COMPLEX&AMP;&NBSP;C2) {     if  (C1.R==C2.R  &&  (c1.i==c2.i))  {         return true;    }    else{     & NBsP;  return false;    }}bool operator!= (CONST&NBSP;COMPLEX&AMP;&NBSP;C1 , CONST&NBSP;COMPLEX&AMP;&NBSP;C2) {    if  ((C1.R==C2.R)  &&  (c1.i== C2.I))  {        return false;    }     else{        return true;     }}


  main.cpp//  train5////  created by student on 15/8/11.   Copyright  (c)  2015 year  personals. all rights reserved.//#include  <iostream> #include   "Complex.h" Int main (int argc, const char *  Argv[]) {&NBSP;&NBSP;&NBSP;&NBSP;//&NBSP;INSERT&NBSP;CODE&NBSP;HERE...&NBSP;&NBSP;&NBSP;&NBSP;COMPLEX&NBSP;C1 ( 1, 3), C2 (4,5);    complex c;    c1+=c2;     cout<<c1;    c1-=c2;    cout<<c1;     c1*=c2;    cout<<c1;    c1/=c2;    cout< <c1;    c1--;    --c1;    cout<<c1;     if  (C1==C2)  {        cout<< "c1==c2\n ";    &nBSP;}     else if  (C1!=C2) {        cout< < "c1!=c2\n";    }    //std::cout <<  "Hello,  World!\n ";     return 0;}


This article is from the "c0c0s" blog, make sure to keep this source http://9362125.blog.51cto.com/9352125/1684874

Operator overloading in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.