Function overloading in c++

Post on 08-Jan-2017

555 views 0 download

Transcript of Function overloading in c++

Yogi’s Guide to

C++Yogi’s Guide

toC++

Function Overloading

Yogendra Pal

www.learnbywatch.com | yogendra@learnbywatch.com

At the end of this tutorial you will be able to • Explain function signature.

• Differentiate functions on the basis of their signature.

• Explain function overloading.

• Identify overloaded functions from a given c++ program.

• Identify which function will execute on a call to overloaded function.

• Identify an ambiguous function call.

• Write overloaded functions in a c++ program.

www.learnbywatch.com | yogendra@learnbywatch.com

Function Overloading / Function Polymorphism• Polymorphism (बहुरूपता) means having many forms.

• Function polymorphism: A function in many forms.

• The process of creating same named functions is called function overloading.

• Such functions are called overloaded functions.

• In C++ two functions can have same name as long as their signatures are different.

www.learnbywatch.com | yogendra@learnbywatch.com

Function Signature / Argument List• Argument list of a function defines it’s signature.

• Signature of a function is different if any one of the following is different.• Number of arguments,

• Data Type of arguments and

• Order of arguments.

void setTime( ); void setTime(int hh); void setTime(int hh, double mm);

void setTime(double hh); void setTime(double mm, int hh);

www.learnbywatch.com | yogendra@learnbywatch.com

Call an overloaded function• void setTime( );

• void setTime(int hh);

• void setTime(int hh, double mm);

• void setTime(double hh);

• void setTime(double mm, int hh);

• setTime(12.5)

• setTime(12.5 , 7)

• setTime()

• setTime(7, 12.5)

• setTime(7)

www.learnbywatch.com | yogendra@learnbywatch.com

Ambiguous Call• void setTime(int hh, double mm);

• void setTime(double mm, int hh);

• setTime(5, 1.2)

• setTime(4.2, 5)

• setTime(2, 3)

• setTime(2.1, 4.5)

Yogi’s Guide to

C++Yogi’s Guide

toC++

Ask your questionsto learn better

Yogendra Pal

www.learnbywatch.com | yogendra@learnbywatch.com