0% found this document useful (0 votes)
3 views21 pages

Function Overloading

The document covers concepts of function and operator overloading in C++, including definitions, rules, and examples. It explains polymorphism, distinguishing between compile-time and runtime polymorphism, and discusses constructor overloading. Additionally, it highlights the advantages and disadvantages of function overloading, along with practical programming exercises related to calculating areas and searching employee records.

Uploaded by

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

Function Overloading

The document covers concepts of function and operator overloading in C++, including definitions, rules, and examples. It explains polymorphism, distinguishing between compile-time and runtime polymorphism, and discusses constructor overloading. Additionally, it highlights the advantages and disadvantages of function overloading, along with practical programming exercises related to calculating areas and searching employee records.

Uploaded by

Pranav Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CI215:Object Oriented Design

and Programming Lab

[Link]. Pragati P. Patil


3. Functions & Operator Overloading

• Function Overloading
• Operator Overloading
• Copy Constructors & Default Arguments: Function
overloading
• Overloading constructor function
• Copy constructors
• Operator overloading using friend function.
Polymorphism

• The word polymorphism is derived from Greek word Poly which means
many and morphos which means forms.
• Polymorphism can be defined as the ability to use the same name for two or
more related but technically different tasks.
• Eg. Person plays role of daughter, son, teacher, etc.

• Overloading:
Overloading means assigning multiple meaning to a function name or operator
symbol.
Types of Polymorphism in C++

1. Compile Time Polymorphism


• In compile-time polymorphism, a function is called at the time
of program compilation. We call this type of polymorphism as
early binding or Static binding.
• Function overloading and operator overloading is the type of
Compile time polymorphism.
2. Runtime Polymorphism
• In a Runtime polymorphism, functions are called at the time
the program execution. Hence, it is known as late binding or
dynamic binding.
• Function overriding is a part of runtime polymorphism. In
function overriding, more than one method has the same
name with different types of the parameter list.
Def: Group of statements that together perform a task
Function
Syntax:

return_type function_name( parameter list )


{
//body of the function

Example:

No parameter With Parameter

int add() int add(int x, int y)


{ {
int x,y,z; int z;
z=x+y; z=x+y;
return z; return z;
} }
Function Overloading

• Overloading means use of the same thing for different purposes.


• Function overloading is a C++ programming feature that allows us to have more
than one function having same name but different parameter list
• Functions having different number or type (or both) of parameters are known as
overloaded functions.
• Overloaded declaration is declared with the same name as a previously
declared declaration in the same scope
• Both declarations have different arguments and obviously different definition
• Two functions can have the same name if the number and/or type of arguments
passed is different.
• These functions having the same name but different arguments are known as
overloaded functions. For Example:
• Overloaded functions may or may not have
different return type but it should have different
argument(s).

• The number and type of arguments passed to


these two functions are same even though the
return type is different. Hence, the compiler will
throw error.
Rules of Function Overloading
• These functions have different parameter type
– sum(int a, int b)
– sum(double a, double b)

• These functions have a different number of parameters


– sum(int a, int b)
– sum(int a, int b, int c)

• These functions have a different sequence of parameters


– sum(int a, double b)
– sum(double a, int b)

• The above three cases are valid cases of overloading. We can


have any number of functions, but remember that the
parameter list must be different.
Function Overloading
1. Functions have different parameter types
• Every function can have arguments of different data types. For example :
// Function overloaded with different types of parameters.
int multiple(int a) {}
int multiple(double a) {}

2. Different number of parameters


• Functions can have different number of parameters for the compiler to
differentiate between them like
// Function overloaded with a different number of parameters.
int sum(int a, int b) {}
int sum(int a, int b, int c) {}

3. Functions have different combinations of arguments


• Two or more functions can be overloaded if they have the same arguments but in
different combinations, for example :
// Function overloaded with different combinations of parameters.
int divide(int a, float b) {}
int divide(float a, int b) {}
#include <iostream>
using namespace std;
class Addition
{
public:
int sum(int a,int b)
{
return a+b;
} Output :
int sum(int a,int b, int c) 35
{ 191
return a+b+c;
}
};
int main()
{
Addition obj;
cout<<[Link](20, 15)<<endl;
cout<<[Link](81, 100, 10);
return 0;
}
Constructor Overloading
• In C++, We can have more than one constructor in a class with same name,
as long as each has a different list of arguments.
• This concept is known as Constructor Overloading.

Constructor
Function Overloading vs Constructor
Overloading
• Functions are the building blocks and Constructor is a
special method that have same name as that of class.
Constructors are invoked at the time of object creation.
• Constructor overloading - while creating an object,
relevant constructor is chosen based on the
parameters. And is called only when you create an
object.
• Function overloading - when a method is called,
relevant method is chosen among the overloaded
methods based on parameters.
• Both are almost same and serve the same purpose.
#include<iostream>
using namespace std;
// Function Overloading
int sum(int a,int b)
{
return a+b;
} Output
int sum(int a,int b,int c) Total : 30
{ Total : 60
return a+b+c;
Total : 35.35
}
float sum(float a,float b)
{
return a+b;
}
int main()
{
cout<<"Total : "<<sum(10,20)<<endl;
cout<<"Total : "<<sum(10,20,30)<<endl;
cout<<"Total : "<<sum(10.25,25.10)<<endl;
return 0;
}
Advantages of Function Overloading

• The program is very simple and also easier to


understand.
• It saves the memory space, maintains consistency,
makes clear interface for methods regardless of the
parameter’s type and readability of the program.
• Using the function overloading concept, we can
develop more than one function with the same name,
but the arguments passed should be of different types.
• Function overloading executes the program faster.
• Function overloading is used for code reusability and to
save memory.
Disadvantages of Function Overloading

• Function declarations that differ by their


return type cannot be overloaded with the
function overloading process.

• If any static member function is declared, then


the same parameters or the same name types
cannot be overloaded.
Q. Write a program to implement function overloading.
Create a 4 functions as area. Demo having same name and
prototype in operation as given below:
[Link] area(int s); //area of square
[Link] area(float r)// area of circle
[Link] area(int l,int b) //area of rectangle
[Link] area(float b,float h) //area of triangle.

Q. Write a C++ program to calculate the area of triangle,


rectangle and circle using function overloading. The program
should be menu driven.
switch(ch){
#include<iostream> case 1:
#include<cstdlib> {
using namespace std; cout<<"\n Enter the Radius of Circle : ";
float area(float r) cin>>r;
{ cout<<"\n Area of Circle : "<<area(r);
return(3.14 * r * r); break;
} }
float area(float b,float h) case 2:
{ {
return(0.5 * b * h); cout<<"\n Enter the Base & Height of Triangle : ";
} cin>>b>>h;
float area(float l,float b) cout<<"\n Area of Triangle : "<<area(b,h);
{ break;
return (l * b); }
} case 3:
int main() {
{ cout<<"\n Enter the Length & Bredth of Rectangle : ";
float b,h,r,l; cin>>l>>b;
int ch; cout<<"\n Area of Rectangle : "<<area(l,b);
do break;
{ }
cout<<"\n\n *****Menu***** \n"; case 4:
cout<<"\n 1. Area of Circle"; exit(0);
cout<<"\n 2. Area of Triangle"; default:
cout<<"\n 3. Area of Rectangle"; cout<<"\n Invalid Choice... ";
cout<<"\n 4. Exit"; }
cout<<"\n\n Enter Your Choice : "; }while(ch!=4);
cin>>ch; return 0;
}
• Write a C++ program to accept records of 'n' employee and store it in an
array. Class employee contains eno, ename[20], salary attributes. Overload
search() function as follows:
a. int search(employee e [], int n, int eno);
b. int search(employee e [], int n, char ename[]);
c. int search(employee e [], int n, float salary);
Which displays all employees whose sal > salary.

You might also like