C + +: operator overloaded functions with member operator overloading

Source: Internet
Author: User
Tags function prototype

5.2.3 member operator overloaded functions

in C + +, operator overloading functions can be defined as member functions of a class, called Member operator overloading functions.

1. Define the syntax form of the member operator overloaded function
(1) Inside the class, the format of the member operator overload function is defined as follows:
function type operator operator (formal parameter list)
{
function Body
}

(2) member operator overloading functions can also declare a prototype of a member function in a class, defined outside the class.

inside the class, the format of the declaration member operator overloaded function prototype is as follows:
class x{
      ...
function type operator operator (parameter table);
};
in the outer definition of the class, the format for defining the member operator overloaded function prototype is as follows:
function type operator operator (parameter table)
{
function Body
}

where x is the class name of the class where the operator of the friend function is overloaded, the function type specifies the return value type of the member operator function, operator is the keyword that defines the operator overload function, the operator is the name of the operator to be overloaded, and must be an overloaded operator in C + + The parameters and types required for overloaded operators are given in the formal parameter list. Because the member operator overload function is a member function of the class, it must be prefixed with the class name when it is defined outside the class.

Note: In the formal parameter list of the member operator overloaded function, if the operator is single-purpose, the parameter table is empty, and if the operator is binocular, there is an operand in the parameter table.

2. Binocular operator Overloading

for binocular operators, there is only one parameter in the formal parameter list of the member operator overloaded function, which acts as the right operand of the operator. The other operand (left operand ) is implied and is the current object of the class, which is implicitly passed to the function through the this pointer. For example

CALSS x{
       int operator+ (X a);
};

Example 5.5 uses the member operator overload function for complex number operations.
Addition: (A+BI) + (C+di) = (a+c) + (b+d) I
Subtraction: (A-BI) + (C-di) = (a-c) + (b-d) I
Multiplication: (A-BI) * (C-di) = (AC-BD) + (AD+BC) I
Division: (A-BI)/(C-di) = (a+bi) * (C-DI)/(C*C+D*D)

#include <iostream>using namespacestd;classcomplex{ Public: Complex () {}; Complex (DoubleRDoublei) {real=R; Imag=i; }    voidprint (); Complexoperator+ (Complex co);//overloaded functions that declare operator +Complexoperator-(Complex co);//overloaded functions that declare operators-Complexoperator* (Complex CO);//overloaded functions that declare operators *Complexoperator/(Complex co);//declaring an operator/overloaded function  Private:    DoubleReal//the real part of the plural    DoubleImag//imaginary part of a complex number}; Complex Complex::operator+ (Complex Co)//defining overloaded functions for operator +{Complex temp; Temp.real= real+co.real; Temp.imag= imag+Co.imag;returntemp; }complex Complex::operator-(Complex Co)//overloaded functions that define operators-{Complex temp; Temp.real= real-co.real; Temp.imag= imag-Co.imag;returntemp; } Complex Complex::operator* (Complex CO)//overloaded functions that define operator *{Complex temp; Temp.real= real*co.real-imag*Co.imag; Temp.imag= real*co.imag+imag*co.real;returntemp;} Complex Complex::operator/(Complex CO)//to define an operator/overload function{Complex temp;Doublet; t=1/(co.real*co.real+co.imag*Co.imag); Temp.real= (REAL*CO.REAL+IMAG*CO.IMAG) *t; Temp.imag= (CO.REAL*IMAG-REAL*CO.IMAG) *T;returntemp;} voidComplex::p rint () {cout<<Real; cout<<"+"<<imag<<'I'<<Endl;} intMain () {Complex A1 (2.3,4.6), A2 (3.6,2.8), A3,a4,a5,a6; A3= A1+A2;//A3 = a1.operaotr+ (A2)A4 = A1-A2;//A3 = a1.operaotr-(A2)A5 = A1*A2;//A3 = a1.operaotr* (A2)A6 = A1/A2;//A3 = a1.operaotr/(A2)A1.print (); A2.print (); A3.print (); A4.print (); A5.print (); A6.print (); return 0; } /*In general , if you use member functions in class X to overload the binocular operator @, the member operator function [email protected] requires an operand that is implicitly passed by the object AA through the this pointer, and another operand of it is shown in the parameter table, the following        The two methods of function invocation are equivalent.                 [Email protected];      implicit call [email protected] (BB); Show Call*/ 

3. (Member operator overloaded function) Monocular operator overload

For the monocular operator, there are no parameters in the parameter table of the member operator overloaded function, at which point the current object is an operand of the operator.

Example 5.6 overloading the single-mesh operator with the member function "+ +"

#include <iostream>using namespacestd;classcoord{ Public: Coord (intI=0,intj=0) {x=i; Y=J; } Coordoperator++();//declaring member operators + + overloaded functions//void operator++ ();  voidprint ();Private:  intx, y;}; Coord Coord::operator++()//Define member Operators + + overloaded functions{ ++x;++y;return* This;//returns the value of the current object}/*void coord::operator++ () {++x; ++y;}*/voidCoord::p rint () {cout<<"x="<<x<<","<<"y="<<y<<Endl;}intMain () {Coord C (Ten, -);  C.print (); ++c;//Implicit invocationC.print (); C.operator++();//Show CallC.print (); return 0;}/*In this case, the two ways to invoke the member operator overload function operator in the main function are equivalent.             The ++c ========== c.operator++ () is in the form of: @aa;   implicit invocation of Aa.operator (); Display calls from this example, it can also be seen that when a single-mesh operation is overloaded with member functions, no parameters are displayed to the member operator. Arguments are implicitly passed to the function through the this pointer*/

C + +: operator overloaded functions with member operator overloading

Related Article

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.