• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

C++ Function Overloading

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);

}

Summary
Review Date
Reviewed Item
C++ Function Overloading
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on C++ Function Overloading"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Site Disclaimer, Copyright © 2016 - All Rights Reserved.

    Support


    Please leave a message and we'll get back to you soon.

    I agree to be contacted via e-mail.