Session Objectives
Explain Overloading
Explain Member Function Overloading
member Function Overloading with default Args.
Overloading
• Another powerful and useful concept of object
oriented programming is overloading
• which allows operation with the same name but
different semantics and different ways of
implementation to be invoked for object of differnt
types.
• For instance,binary operation can be overloaded for
complex number arrays,set or list
Function Overloading
• C++ enable two or more functions with the same
name but with different types of arguments or different
sequence of arguments or different number of arguments. It is
also called as “Function Polymorphism”
Two or more functions with the same
name but with different types of arguments
MEMBER FUNCTION void display(char c, int n)
OVERLOADING {
#include<iostream.h> for (;n>0;n--)
#include<conio.h> cout<<c<<" ";
void display(char,int); cout<<endl;
void display(int,int); }
void main() void display(int a, int b)
{ {
for (;b>0;b--)
clrscr();
cout<<a<<" ";
display('*',10);
cout<<endl;
display(10,5); }
getch();
} *********
10 10 10 10 10
Two or more functions with the same
name but with different Number of
arguments
cin>>breadth;
// Write a C++ program to find
area=length*breadth;
the area of the triangle using
}
Memeber Function Overloading
void showdata(){
#include<iostream.h>
cout<<"Length is:"<<length;
class rectangle
cout<<"Breadth is :"<<breadth;
{
cout<<"The Area is :"<<area;
private :
}
int length,breadth,area;
};
public :
void main()
void getdata() {
cout<<"Enter Length Value :";
{
cin>>length; rectangle r1,r2;
cout<<"Enter breadth Value :”; cout<<"Enter the detail "<<endl;
cin>>breadth; r1.getdata();
area=length*breadth; } r1.showdata();
void getdata(int a) cout<<"Enter the detail"<<endl;
{ r2.getdata(5);
length=a; r2.showdata();
cout<<"Enter breadth Value :"<<endl; }
Two or more functions with the same
name but with different Sequence of
arguments
#include<iostream.h> void line()
#include<conio.h> {
void line(); for(int i=0;i<15;i++)
void line(char); {
void line(char,int);
cout<<"&";
void main()
}
{
int no_of_time; cout<<"\n";
char c; }
clrscr();
cout<<"\n Enter the no of times:";
cin>>no_of_time;
cout<<"\n Enter the character:";
cin>>c;
line();
line(c);
line(c,no_of_time);
getch();
}
Member Function Overloading with
Default Arguments
The default values are given in the
function prototype declaration .whenever a call
is made to the function without specifying on
arguments the program will automatically
Assign values to the parameter form the
default function prototype declaration
Member function Overloading with default arguments
#include<iostream.h>
#include<conio.h>
int sum(int x=10,int y=20,int z=30);
void main()
{
clrscr();
cout<<"sum()="<<sum()<<endl;
cout<<"sum(15)"<<sum(15)<<endl;
cout<<"sum(15,25)"<<sum(15,25)<<endl;
cout<<"sum(15,25,35)"<<sum(15,25,35)<<endl;
getch();
}
int sum(int x,int y,int z)
{
return(x+y+z);
}
Difference between Overloading
and Overriding
Allows a function to be given more
than one definition
Overloading
The types of arguments or number
of arguments which the function
takes determines which definition
will be used
Overriding
It is the ability to change the
definition of the base class method
by writing a code for them in the
sub-class of an inheritance
Function Overloading is the ability to write more than on
function with the same name with different number ,type and
sequence of arguments. It is also referred as “Function
polymorphism”
EXERCISES
1. Define Function Overloading?
2. Explain Function overloading with different number of
arguments with an example?
3. Explain Function overloading with different types of
arguments with an example?
4. Explain Function overloading with different sequence of
arguments with an example?