Sie sind auf Seite 1von 15

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
types.

semantics

and

different

ways

of

implementation to be invoked for object of differnt 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 OVERLOADING


#include<iostream.h> #include<conio.h> void display(char,int); void display(int,int); void main() { clrscr(); display('*',10); display(10,5); getch(); }

void display(char c, int n) { for (;n>0;n--) cout<<c<<" "; cout<<endl; } void display(int a, int b) { for (;b>0;b--) cout<<a<<" "; cout<<endl; }

********* 10 10 10 10 10

Two or more functions with the same name but with different Number of arguments

// Write a C++ program to find the area of the triangle using Memeber Function Overloading #include<iostream.h> class rectangle { private : int length,breadth,area; public : void getdata() {

cin>>breadth; area=length*breadth;

} void showdata(){ cout<<"Length is:"<<length; cout<<"Breadth is :"<<breadth; cout<<"The Area is :"<<area; } }; void main() { cout<<"Enter Length Value :"; rectangle r1,r2; cin>>length; cout<<"Enter the detail "<<endl; cout<<"Enter breadth Value :; 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> #include<conio.h> void line(); void line(char); void line(char,int); void main() { int no_of_time; 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(); }

void line() { for(int i=0;i<15;i++) { cout<<"&"; } cout<<"\n"; }

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


default function prototype declaration

form the

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


arguments with an example?

types of

4.

Explain Function overloading with different sequence of arguments with an example?

Das könnte Ihnen auch gefallen