Write a C++ program to implement the concept of Function Overloading.
The algorithm to solve the above problem:
1. Start the program.
2. Create the class printData and functions.
3. In the main(),create the objects.
4. Use print() function to print different values.
5. The values that are passed inside the function call will be matched with the definition part and appropriate calculations are done.
6. Stop
Implementation of the above problem in C++:
#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;
}
void print(char* c)
{
cout << "Printing character: " << c << endl;
}
};
int main()
{
printData pd;
pd.print(5); // Call print to print integer
pd.print(10.5); // Call print to print float
pd.print("Hello C++"); // Call print to print character
return 0;
}
The output of the above program is tested on www.jdoodle.com
Output:
Printing int: 15
Printing float: 100.5
Printing character: Hello C++
Thanks
Mukesh Rajput
Post A Comment:
0 comments: