Beruflich Dokumente
Kultur Dokumente
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
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(); }
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); }
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 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.
3.
types of
4.