C++ Function Overloading

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

Function Overloading

Defining more than one function with same name by changing -->number of parameters -->types of parameters --?order of parameters is called function overloading in C++ Function is overloaded when more than one function perform similar operation with different implementation A group of functions which perform similar operation refer with one name. overloaded function having polymorphism behaviour This is called name polymorphism There should not be any two functions with same  

What is name Mangling

It is a process of giving unique name to over loaded function by compiler Compiler gives unique name each every overloaded function of linker Linker recognize the function with name

Example

 prog1.cpp

void fun1 (int x, int y) { cout<<x<<y; }

void fun1(float x,float y) { cout<<x<<y; }

void main() { func1(10,20); func1(1.5,2/5); }  

prog1.obj

void fun1_int(int x, in) { cout<<x<<y; }

void fun1_float(float x,float) { cout<<x<<y;

void main() { fun1_int(10,25); func1_float(1.5); }  

Example # include<iostream.h> void sor(int a[],int size) { for(int i=0;i<size;i++) for(int j=0;j<size-i;j++) { if a[j]>a[j+1] { int t=a[j]; a[j]=a[j+1];   Linker  

Prog1.exe

void main() { fun1_int(10,20) fun1_float(1.5); }

void fun1_int(int x,int y) { } void fun1_float (floatx.float) }

void sort(float a[], int size) { for (int i=0; i<size; i++) for (int j=0; j<size-i;j++) { if(a[j]>a[j+1]) { float t=a[j]; a[j]=a[j+1]; a[j+1]=l; } } }

void main() { int a[]={5,2,4}; float b[]={1.5,2.5,1.2}; sort(a,3); sort(b,3); int i; for(i=0;i<3;i++) cout<<endl<<a[i]<<”lt”<<b[i]; } }

void sort(float a[], int size) { for (int i=0; i<size; i++) for (int j=0; j<size-i;j++) { if(a[j]>a[j+1]) { float t=a[j]; a[j]=a[j+1]; a[j+1]=l; } } }

void main() { int a[]={5,2,4}; float b[]={1.5,2.5,1.2}; sort(a,3); sort(b,3); int i; for(i=0;i<3;i++) cout<<endl<<a[i]<<”lt”<<b[i]; }  

Example of  Function Overloading in C++

# include <iostream.h>

float find-area(float.h) { return 3.147*r*r; }

float find-are(float b, float h) { return 0.5*b*h; } void main() { cout <<”n1.Area of circle”; cout<<”n2. Area of Triangle”; cout<<”\n input your option”; int opt; cin>> opt; switch(opt) { case 1 : float r; cout<<”\n input r”; cin >> r; cout<< “\n Area of circle is ”<< find-area®; break; case 2: float b,h; cout<<”\n input b,h”; cin>>b>>h; cout<<”\n Area of triangle is ”<< find –area(b) brak; default: cout<<  “\n invalid option”; } }  

// find output ?

  # include <iostream.h>

void func1(int x) { cout<<”\n inside one parameter function”; }

void func1(int x, int   y=0) { cout<< “\n inside two parameter function”; }

void main() { fun1(10,20); func1(30); }  

Explanation

The above program displaying compile time error. Because there are two functions which match with function call sending one argument eg -1 : # include <iostream.h>    fun c overloading void print(int x) { count <<”\n inside inter print function”; }

void print(float x) { cout<< “ \n in each “ }  

void print(double x) { cout<<”\n inside double print funciton”; }

void main() { cout<<”\n inside double print function”; }  

void main() { clrscr(); print(10); print(1.5);    //(double value) print(2.5f);   // (floating value) }  

pass by address in C++

Calling on invoking function by sending address on pointer is called pass by address. In pass by address function parameters are declared of type pointer.

Advantages of pass by address in C++

Sharing data between the functions. Increase efficiency of program Note :

  • Invoking function by sending array is implicitly sending address.

eg : # include <iostream.h> void swap(int *p,int *q) { int l=*p; *p=*q; *q=t; }  

Screenshot_10

void main() { int x,y; cout<<”\n input x,y values”; cin>>x>>y; swap(&x,&y); count<<”\n after swapping \n”; count<<”\n <<x   <<” \t “” <<y; } o/p : input x,y values 10         20 After swapping 20 10  

// write a function to copy contents of one string to another string without using strcpy()

# include <iostream.h> # include<conio.h> void string_copy(char *dest, char  *src) { for(int i=0; src[i]!=’10’;i++) dest[i]=scr[i]; dest[i]=’10’; } void main() { char str1[10],str2[20]; clrscr(); cout<<”\n input any string”; cin>> str1; string_copy (str2,str1); string_copy(str2,str1); cout<<str1<<endl; cout<<str2<<endl; } o/p : input any string nit after copying nit nit    

Explanation:

Screenshot_11    

String – copy(char*des, char *sre) { For(int i=0; src[i]l=’10’,i++); Dst[i]=src[i]; Dest[i]=’10’ }

Screenshot_12

Pass by reference/ Alias in C++

Calling or invoking function by sending value type In pass by reference function parameters are declared reference type. This allows to share data between functions.

Example # include<iostream.h> void swap(int &x, intt &y) { x=x+y; y=x-y; x=x-y; } void main() { int a,b; cout<<”\n input a,b values”; cin>>a>>b; swap(a,b); cout<<”\n after swapping \n”; cout<<a<<”\t”<<b; }

Screenshot_13

o/p : 0     10

eg : # include<iostream.h> void func1(int x) { cout<<”\n inside pass by value”; } void func1 (int y) { cout<<” \n inside pass by alias” ; } void main() { int a =10; fun1(a); }      

Explanation

The above program display compile time error because the syntax of calling function by sending value and alias is same. The function with value type parameters can’t be overloaded function with pointer type parameter

Example

# include<iostream.h>

void func1(int x)

{ cout<<”\n inside value type parameter” }

void func1(int *y)

{ int a=10; func1(&a) ; fun1(a); }

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox