function overloading of C + +

Source: Internet
Author: User

Overloads of C + +

In some programming languages, function overloading or method overloading are the ability to create multiple methods of the Same name with different implementations. Calls to an overloaded function would run a specific implementation of that function appropriate to the context of the call , allowing one function call to perform different tasks depending on context.

For example, Dotask () and Dotask (object O) is overloaded methods. To call the latter, an object must being passed as a parameter, whereas the former does not require a parameter, and is Calle D with an empty parameter field. A common error would is to assign a default value to the object in the second method, which would result in an ambiguous C All error, as the compiler wouldn ' t know which of the the and methods to use.

Another appropriate example would be a Print (object O) method. In this case one might like the method to being different when printing, for example, text or pictures. The different methods may be overloaded as Print (Text_object T); Print (Image_object P). If we write the overloaded print methods for all objects our program would "print", we never has to worry about the type O f the object, and the correct function call again, which is always:print (something).

The following program implements overloading of functions.

#include <stdlib.h> #include <iostream>using namespace std;void fun (int i=30, int j=20, int k=10), int main (voi D) {fun (); fun (+); Fun (+, +); System ("pause"); return 0;} void Fun (int i, int j, int k) {cout << i << "," << J << "," << K<<endl;}

program, the same name function, the number of function parameters are different, run the result:

function overloading of C + +

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.