function overloading for C + +

Source: Internet
Author: User

1. Overloading: A function overload is a function that assigns new meanings to existing functions and enables them to implement new functions, so that the same function name can be used instead of functions. 2. How do I resolve a naming conflict issue when declaring and defining function overloading? Using is a method of resolving naming conflicts 3  operator overloading is essentially the function of overloading overloaded operators with functions such as: function type  operator  operator name (formal parameter list) {operator overloading} For example: Want to + For the addition operation of complex, the prototype of the function can be this: complex operator+ (COMPLEX&NBSP;&AMP;C1,COMPLEX&NBSP;&AMP;C2); In the general format above, operator is a keyword that is specifically used to define operator overloading functions, the function name consists of the operator and the operator operator+, and two parameters are references to complex class objects that require arguments to be complex objects. 4. Focus on user experience there may be some doubts that, since operator overloading is as useful as function overloading, what about operator overloading? This takes into account the user experience, from the user's point of view although the functions implemented by operator overloading can be implemented entirely by functions, using operator overloading can make the user program easy to write, and read Maintenance 5. Rules for overloaded operators cannot be overloaded with five operators: (1). (Member access operator) (2) * (pointer access operator) (3):: (Domain operator) (4) sizeof () (length operator) (5)?: (conditional operator)    first two operators cannot be overloaded in order to access the members of the function cannot be changed, The domain operation conforms to the sizeof operator's operand type, not to a variable or generic expression, without overloading the features    overloads cannot change the number of operator operands, cannot change the precedence of operators, cannot change the tuberculous nature of operators, overloaded operators cannot have default parameters  6 Why does the operator overload function have only one parameter?   Actually the operator overload function should have two arguments, but the overloaded function is a member function in the complex class, so one argument is implied, and the operator function implicitly accesses the members of the object with the this pointer, for example, here  complex  operator+ (COMPLEX&NBSP;&AMP;C2)   You can see that operator+ accesses two object members, one of which is a member of the parameter object This->real+c2.real,this->real is  c1.real. #include <iostREAM&GT;USING&NBSP;NAMESPACE&NBSP;STD;CLASS&NBSP;COMPLEX{PUBLIC://defines the constructor complex (int real=0,int image =0) {_real=real;_image=image;} 1 declares the complex addition function complex operator+ (COMPLEX&NBSP;&AMP;C2) { complex c; c._real=_real+c2._real;  c._image=_image+c2._image; return c;} 2 declaration + = function complex &operator+= (const complex &c) {this->_real+=c._real;this->_image+ =c._image;return *this;} 3 Declaration Pre + + function complex &operator++ () {this->_real++;this->_image++;return *this;} 4 Declaration Post + + function complex operator++ (int) {complex tmp (*this); This->_real++;this->_image++;return  tmp;} 5 Declaration-Function complex operator-(COMPLEX&NBSP;&AMP;C2) {complex c;c._real=_real-c2._real;c._image=_image-c2. _image;return c;} 6 declaration * Function complex operator* (COMPLEX&NBSP;&AMP;C2) {complex c;c._real=_real*c2._real-_image*c2._image;c. _image=_image*c2._real+_real*c2._image;return c;} Defines the complex output function void display () {cout<<_real<<"+" <<_image<< "I" &LT;&LT;ENDL;} private:int _real;int _image;}; Int main () {//test1complex c1 (3,4), C2 (5,6), c3;//define 3 complex object c3=c1.operator+ (C2);//call complex add function cout<< "C1 = "; C1.display ();cout<<" c2= "; C2.display ();cout<<" c1+c2= "; C3.display ();////TEST2//COMPLEX&NBSP;C1 ( 3,4), C2 (2,1),//c1=c1.operator+= (C2);//cout<< "c1=";//c1.display ();//////test3//complex c1;//c1= c1.operator++ ();//cout<< "c1=";//c1.display (;////test4//complex c1);//c1=c1.operator++ ();//cout << "c1=";//c1.display ()//////test5//complex c1 (3,4), C2 (c3;//c3=c1.operator-) c2 "C1-c2=";//c3.display ();//////test6//complex c1 (n), C2 (2,3), c3;//c3=c1.operator* (C2);//cout<< "c1* C2= ";//c3.display (); System (" pause "); return 0;}


function overloading for 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.