C + + polymorphism (ii)--function overloading (overloading) and operator overloading

Source: Internet
Author: User

any function can be overloaded. I. Overloading of common functions

A function in C can handle only one type of data, not two or more data types; C + + uses a function with the same name to handle multiple types of data.

#include <iostream>#include<vector>using namespacestd;DoubleSqDoubleY//fun1{    returny*y;}intSqintY//fun2{    returny*y;}DoubleSqintY//Fun3{    return(Double) y*y;}voidMain () {intI=5; DoubleD=5.5; cout&LT;&LT;SQ (i) <<Endl; cout&LT;&LT;SQ (d) <<Endl;}

The fun2 and fun3 conflicts, because the parameters list of the two are the same, and the overloaded conditions are not met. If you erase FUN3, get answers 25 and 30.25.

Therefore, overloading is based on the parameter list .

Second, overloading of the constructor function
#include <iostream>using namespacestd;classa{intA, B; Public: A (); A (intIintj); ~A (); voidSet (intIintj) {a=i; b=J;}}; A::a () {A=b=0; cout<<"no_arg_cons:a="<<a<<", b="<<b<<Endl;} A::a (intIintj) {a=i; b=J; cout<<"constructor:a="<<a<<", b="<<b<<Endl;} A::~A () {cout<<"destructor:a="<<a<<", b="<<b<<Endl;}voidMain () {cout<<"Starting first round:\n"; A obja[3];  for(intI=0; i<3; i++) Obja[i]. Set (i, I+1); cout<<"Finishing First round:\n"; cout<<"starting second round:\n"; A objb[3]={a (5,6), A (7,8), A (9,Ten)}; cout<<"finishing second round.\n"; cout<<"Program being terminated!\n";}

Result:
Starting first round:no_arg_cons:a=0, b=0no_arg_cons:a=0, B=0no_arg_cons:a=0, b=0finishing first round:Starting second Round:constructor:a=5, B=6constructor:a=7, B=8constructor:a=9, b=10finishing second round. Program being terminated! Destructor:a=9, B=10destructor:a=7, b=8destructor:a=5, b=6destructor:a=2, B=3destructor:a=1, b=2destructor:a=0, b=1< /c5>

Threeoperator Overloading

In object-oriented programming languages, operators are also functions, so operators can overload them like functions to do more.

Operator overloading is implemented in two ways, one as a friend function and the other as a member function.

1. Friend function mode

#include <iostream>using namespacestd;classpoint{intx, y; Public: Point (intvx=0,intvy=0) {x=VX; Y=VY; } Friend Pointoperator+ (Point & P1, point &p2); Friend Pointoperator-(Point & P1, point &p2); voidprint () {cout<<x<<" "<<y<<Endl; }};p ointoperator+ (Point & P1, point &p2)    {point P; P.x= p1.x +p2.x; P.Y= P1.y +p2.y; returnp;} //cannot return a [local variable] referencePointoperator-(Point & P1, point &p2)    {point P; P.x= p1.x-p2.x; P.Y= P1.y-p2.y; returnp;}voidMain () {point P1 (Ten,Ten), p2 ( -, -);    P1.print (); P1= P1 + P2;//i.e. p1= operator + (p1, p2)P1.print (); P2= P1-P2;//namely p2= operator-(P1, p2)p2.print ();}

2. member function mode

#include <iostream>using namespacestd;classpoint{intx, y; Public: Point () {x=0; y =0; } Point (intVxintvy) {x= VX; y =VY;} Pointoperator+ (Point &p); Pointoperator-(Point &p); voidPrint () {cout<<x<<" "<<y<<Endl;}}; Point point::operator+ (Point &p)    {Point temp; Temp.x= x + p.x;//is temp.x = this->x + p.x;Temp.y = y +p.y; returntemp;} Point point::operator-(Point &p)    {Point temp; Temp.x= X-p.x; TEMP.Y= y-p.y; returntemp;}voidMain () {point P1 (Ten,Ten), p2 ( -, -), P3;    P3.print (); P3= P1 + P2;//i.e. P3 = P1.operator + (p2);P3.print (); P3= P3-P1;//i.e. P3 = p3.operator-(p1);p3.print ();}

25

30.25

C + + polymorphism (ii)--function overloading (overloading) and operator overloading

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.