C++ tutorial-C++ function overloading


C++ function overloading


Overloaded functions are functions that have the same name but different number of parameters and/or data types.

Example:

#include<iostream.h>

using std::endl;

int Max(int val1, int val2){

            if(val1>val2) return val1;

            else return val2;

}

float Max(float val1, float val2, float val3){

            if(val1>val2){

                        if(val1>val3) return val1;

                        else return val3;}

            else if(val2>val3) return val2;

            else return val3;

    }

void main(){

    cout<<"The maximum value among 12, 10, and 20 is:"<<Max(12,10,20)<<endl;

    cout<<"The maximum value among 12.5, 10.5, and 20.5 is:"<<Max(12.5,10.5,20.5)<<endl;

}




Comments

John comment

 John

Troy,
Much thanks for these C++ programming examples! I really appreciate them.


2017-02-08
Troy comment

 Troy

for more C++, C exercises,
please visit http://ccphplusplus.blogspot.com/


2015-05-26



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.