COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button
C++ LANGUAGE FUNCTION OVERLOADING
Function Overloading in C++
You can have multiple definitions for the same function
name in the same scope. The definition of the function
must differ from each other by the types and/or the number
of arguments in the argument list. You cannot overload
function declarations that differ only by return type.
Following is the example where same function print()
is being used to print different data types:
#include <iostream>
using namespace std;
class printData
{
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
25. OVERLOADING (OPERATOR & FUNCTION)
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};
int main(void)
{
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return 0;
}
When the above code is compiled and executed, it produces the following result:
Printing int: 5
Printing float: 500.263
Printing character: Hello C++
No comments:
Post a Comment