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

Function Overloading

Uploaded by

amitdevvv5
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Function Overloading

Uploaded by

amitdevvv5
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

FUNCTION OVERLOADING

By Dr. Ajay Rastogi


POLYMORPHISM
• The term "Polymorphism" is the combination of "poly" + "morphs" which means many forms.
• It is a greek word.
• In object-oriented programming, we use 3 main concepts: inheritance, encapsulation, and
polymorphism.
• Real Life Example Of Polymorphism
• Let's consider a real-life example of polymorphism.
• A lady behaves like a teacher in a classroom, mother or daughter in a home and customer in a
market.
• Here, a single person is behaving differently according to the situations.
• Compile time polymorphism:
• The overloaded functions are invoked by matching the type and number of arguments.
• This information is available at the compile time and, therefore, compiler selects the appropriate
function at the compile time.
• It is achieved by function overloading and operator overloading which is also known as static
binding or early binding.
• Run time polymorphism:
• Run time polymorphism is achieved when the object's method is invoked at the run time instead
of compile time.
• It is achieved by method overriding which is also known as dynamic binding or late binding.
FUNCTION OVERLOADING
• If we create two or more members having the same name but different in number or type of
parameter, it is known as C++ overloading.
• In C++, we can overload:
Methods
Constructors
Types of overloading in C++ are:
• Function overloading
• Operator overloading
• Function overloading is a feature of object oriented programming where two or more functions
can have the same name but different parameters
• When a function name is overloaded with different jobs it is called Function Overloading.
• In Function Overloading “Function” name should be the same and the arguments should be
different.
• It is only through these differences compiler can differentiate between the functions.
• Function overloading can be considered as an example of polymorphism feature in C++.
• 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.
#include <iostream>
using namespace std;
void print(int i) {
cout << " one parameter " << i << endl;
}
void print(int i, int j) {
cout << "two parameters " << i<<" "<<j << endl;
}
void print(char c) {
cout << " one character " << c << endl;
}

int main() {
print(10);
print(10,10);
print(‘S’);
#include <iostream>
using namespace std;
class lpu {
int c;
public:
void add(int a,int b){
c= a + b;
cout<<c<<endl;
}
void add(int a, int b, int c)
{ c= a + b + c;
cout<<c;
}
};
int main(void) {
lpu obj; // class object declaration.
obj.add(10,20);
obj.add(12, 20, 23);

You might also like