Overloading of PHP, C + +

Source: Internet
Author: User

PHP overloading is used in object-oriented classes, which is not the same as C + +, where overloads can be used for process-oriented and object-oriented, and the methods are different. In C + +, overloading applies to cases where the function name is the same but the number of arguments is different, and when the program compiles, the corresponding function is automatically selected based on the number of arguments provided, such as the following section of C + + code:

#include <iostream>using namespace Std;class box{private:    int l,w;public:    void Set () {        l=0; w=0;    }
The overload here is the set () function void set (int ll) { l=ll; w=0; } void set (int ll,int ww) { l=ll; w=ww; } void display () { cout<< "l=" <<l<< ";"; cout<< "w=" <<w<<endl; }; int main () { Box box_1,box_2,box_3; Box_1.set (); cout<< "box_1:\n"; Box_1.display (); Box_2.set (5); cout<< "box_2:\n"; Box_2.display (); Box_3.set (7,9); cout<< "box_3:\n"; Box_3.display ();}

The output is:

Box_1:

L=0;w=0

Box_2:

l=5; W=0

Box_3:

l=7; W=9 you can see from the results that different functions are executed when the number of parameters is different.

Back to the point, PHP overload in my opinion is actually a kind of overwrite (or modify), that is, on the basis of the parent class, modify, give the subclass a new function, the function is not in the parent class. This is also true in C + +, except that it is not overloaded in C + +.

Refer to the code:

<! DOCTYPE html>

Output Result:

I am a student!
I Haven ' t graduate
I was a student before!
But I had graduated!

Overloading of PHP, 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.