0% found this document useful (0 votes)
30 views

C++ Function Overloading

Function overloading allows defining multiple functions with the same name but different parameters. It increases readability and allows a single function name to handle different tasks depending on parameters. There are two ways to implement overloading: different number of parameters and different parameter types. Examples demonstrate overloading to calculate sums and find maximum values using different numbers and types of parameters.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

C++ Function Overloading

Function overloading allows defining multiple functions with the same name but different parameters. It increases readability and allows a single function name to handle different tasks depending on parameters. There are two ways to implement overloading: different number of parameters and different parameter types. Examples demonstrate overloading to calculate sums and find maximum values using different numbers and types of parameters.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Function Overloading

Overloading Concept = > C++ Feature -> Polymorphism (Poly – Many + Morph - Forms)

Function = 1 functionname with different purposes (operations).

Function Overloading = The two or more member functions having the same name but different in
either number of parameters or different types of parameters.

Overloading

1. Function

2. Constructor

3. Operator

Advantage of FO:

It increases the readability of the program.

Ways to implement the FO

1. Different number of parameters

2. Different types of parameters

Method 1 to implement FO - Different number of parameters

Working of Function Overloading

Line 21 -> c.sum(15,5)


int sum(int a, int b) //
Two parameters FO
{
return a+b;
}
int sum(int a, int b, int c)
// Three
parameters FO
{
return a+b+c;
}
Demo Program

#include<iostream>

using namespace std;

class Cal

public:

int sum(int a, int b) // Two parameters FO

return a+b;

int sum(int a, int b, int c) // Three parameters FO

return a+b+c;

};

int main()

Cal c;

cout<<c.sum(15,5)<<endl;

cout<<c.sum(10,30,50);

return 0;

}
Problem Statement: To find maximum of numbers using Function Overloading

#include<iostream>

using namespace std;

int max (int a,int b, int c)

if (a>b && a>c)

return a;

else if (b>a && b>c)

return b;

else

return c;

int max (int a ,int b, int c, int d, int e)

if (a>b && a>c && a>d && a>e)

return a;

else if (b>a && b>c && b>d && b>e)

return b;

else if (c>a && c>b && c>d && c>e)

return c;

else if (d>a && d>b && d>c && d>e)

return d;

else

return c;
}

int main()

cout<<max(30,70,20)<<endl;

cout<<max(30,10,100,70,80);

return 0;

Method 2 : Implement FO using different types of parameters (datatypes)

Char c = ‘a’;

Char c[10] = “RAM”;

// C++ program to display the details using FO with different datatypes

#include<iostream>

using namespace std;

void printchar()

cout<<endl<<"%";

void printchar(char c)

cout<<endl<<c;
}

void printchar(int num, char c)

int i=1;

cout<<endl;

for(i=1;i<=num;i++)

cout<<c;

void printchar(char c,int num)

int i=1;

cout<<endl;

for(i=1;i<=num;i++)

cout<<c;

int main()

{
printchar(); // FC with no parameters

printchar('#'); //FC with 1 parameter - character

printchar(10,'$'); //FC with 2 parameters - int,char

printchar('@',10); // FC with 2 parameters - char, int

return 0;

You might also like