0% found this document useful (0 votes)
26 views19 pages

Oop1 16789

The document discusses function overloading in C++. It defines function overloading as declaring multiple functions with the same name but different parameters. It provides examples of overloading functions based on the number and type of arguments. The advantages of function overloading include improved readability, reusability of code, and faster program execution. However, functions cannot be overloaded based only on their return type. A program example is also provided to demonstrate overloading functions in practice.

Uploaded by

kashafishfaq2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views19 pages

Oop1 16789

The document discusses function overloading in C++. It defines function overloading as declaring multiple functions with the same name but different parameters. It provides examples of overloading functions based on the number and type of arguments. The advantages of function overloading include improved readability, reusability of code, and faster program execution. However, functions cannot be overloaded based only on their return type. A program example is also provided to demonstrate overloading functions in practice.

Uploaded by

kashafishfaq2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

SULPHITES

Group Members:

1. ADIYA MASOORA
2. SAMIA IRSHAD
3. SIDRA BIBI
4.EMAN FATIMA
5. MEERAB IFTIKHAR
ADIYA
MASOORA

Roll no : 18
FUNCTION OVERLOADING
DEFINITION:
The process of declaring multiple functions with same
name but different parameters is called overloading.

Purpose of Function Overloading in C++

It saves memory usage and enables the


reusability of code. It makes the program
execute faster. It is simpler and easier to
understand the code.
SAMIA
IRSHAD
Roll no :11
FUNCTION OVERLOADING

Float Average (int, int)


Float Average (float ,float)
Float Average (int ,float)
•Write any number of function in c++.
Difference:
•Number of Argument
•Type of Argument

void add (int , int ); No. of argument same


void add (int , float ); Type of Argument same

void add (int , int ); No. of argument same Suppose that we call the add function
void add (int , float ); Type of Argument different Add (10, 20);

Add (20 , 10.5);


void add (int , int ); No. of argument different
void add (int , int ,int ); Type of Argument same Add (30 , 12 , 32);
Program:
#include <iostream> return 0;
using namespace std; }

float average(float num1 , float num2)


{
return ( num1 + num2) / 2.0; OUTPUT:
} Average of two numbers: 15.6
float average(float num1 , float num2 , float num3) Average of three numbers: 20.5
{
return (num1 + num2 + num3) / 3.0;
}
int main()
{
float num1 = 10.5 , num2 = 20.7 , num3 = 30.3;
cout << "Average of two numbers:"<<
average(num1 , num2 ) << endl;

cout << "Average of three numbers:"<<


average(num1 , num2 , num3) << endl;
IMPORTANCE OF FUNCTION OVERLOADING

The function overloading in the c++ feature is used to improve the readability
of the code. It is used so that the programmer does not have to remember
various function names. If any class has multiple functions with different
parameters having the same name, they are said to be overloaded.

C++ lets you specify more than one function of the same name in the
same scope. These functions are called overloaded functions, or
overloads. Overloaded functions enable you to supply different
semantics for a function, depending on the types and number of its
arguments
SIDRa BIBI

Roll no:23
ADVANTAGES & DISADVANTAGES

ADVANTAGES OF FUNCTION ADVANTAGES OF FUNCTION


OVERLOADING IN C++ OVERLOADING IN C++

• HELP TO MAINTAIN A CODE. • IN FUNCTION OVERLOADING, THE MAIN


DISADVANTAGE IS THAT THE FUNCTIONS
• IT MAKES THE PROGRAM EXECUTE
WITH DIFFERENT RETURN TYPES CAN'T BE
FASTER. OVERLOADED.
• WITH FUNCTION OVERLOADING THE • E.G
PROGRAMMER CAN DEVELOP
• INT FUN()
FUNCTIONS OF DIFFERENT NATURE
• FLOAT FUN()
BUT WITH THE SAME NAME.
• COMPLEXITY: FUNCTIONS CAN MAKE THE
• IT IS EASY FOR USER TO
CODE MORE COMPLEX BY BREAKING IT
REMEMBER. DOWN INTO MULTIPLE, SMALLER PARTS.
MEERAB
EMAN
IFTIKHAR
FATIMA

Roll no:32
Roll no:36
FUNCTION OVERLOADING THROUGH PROGRAM

PROGRAM:
The parameters should follow any one or more than one of the following
conditions for Function overloading:
•Parameters should have a different type
add(int a, int b)
add(double a, double b)

#include <iostream>
using namespace std;
void add(int a, int b)
{
cout << "sum = " << (a + b);
}
void add(double a, double b)
{
cout << endl << "sum = " << (a + b);
}
// Driver code
int main()
{
add(10, 2);
add(5.3, 6.2);
return 0;
}

Output
sum = 12
sum = 11.5
EMAN
Merab iftikhar
FATIMA
Roll no:36
Roll no:32
PROGRAM:
Following is a simple C++ example to demonstrate function
overloading.
// Importing input output stream files
#include <iostream>

using namespace std;

// Methods to print

// Method 1
void print(int i)
{

// Print and display statement whenever


// method 1 is called
cout << " Here is int " << i << endl;
}
// Method 2 // Method 3
void print(double f) void print(char const* c)
{ {

// Print and display statement // Print and display statement


whenever whenever
// method 2 is called // method 3 is called
cout << " Here is float " << f << endl; cout << " Here is char* " << c <<
} endl;
}
// Method 4
// Main driver method
int main()
{
Output

// Calling method 1 Here is int 10


print(10);
// Calling method 2 Here is float
print(10.10); 10.1
// Calling method 3
print("ten");
Here is char*
ten
return 0;
}

You might also like