You are on page 1of 3

CEC,CGC LANDRAN DEPARTMENT OF IT

Function overloading

Aim – Write a program to demonstrate the use of Function Overloading.

Software and Hardware Used - 12th Gen Intel® Core™ i3-12100 3.30 GHz 64-
bit operating system, x64-based processor.

Theory –

Function Overloading is defined as the process of having two or more function with the same
name, but different in parameters is known as function overloading in C++. In function
overloading, the function is redefined by using either different types of arguments or a different
number of arguments. It is only through these differences compiler can differentiate between the
functions.

The advantage of Function overloading is that it increases the readability of the program


because you don't need to use different names for the same action.

Code -
#include <iostream>

// Function to find the product of two integers


int mul(int a, int b) {
return a*b;
}

// Function to find the sum of three integers


int add(int a, int b, int c) {
return a + b + c;
}

Sakshi singh
2237834
CEC,CGC LANDRAN DEPARTMENT OF IT

// Function to find the sum of two doubles


double add(double a, double b) {
return a + b;
}

int main() {
int p,q,r,s,t;
std::cout<<"Enter the value of a,b,c: "<<std::endl;
std::cin>>p>>q>>r;
std::cout<<"Enter the value of d,e: "<<std::endl;
std::cin>>s>>t;

// Call the appropriate function based on the number of arguments


int prd1 = mul(p ,q);
int sum2 = add(p,q,r);
double sum3 = add(s,t);

std::cout << "Product of " << p << " and " << q << " is " << prd1 << std::endl;
std::cout << "Sum of " << p << ", " << q << ", and " << r << " is " << sum2 << std::endl;
std::cout << "Sum of " << s << " and " << t << " is " << sum3 << std::endl;

return 0;
}

Sakshi singh
2237834
CEC,CGC LANDRAN DEPARTMENT OF IT

Output –

Conclusion – Successfully implemented the program to define the Function Overloading.

Sakshi singh
2237834

You might also like