Function overloading in c++

7
Yogi’s Guide to C++ Yogi’s Guide to C++ Function Overloading Yogendra Pal

Transcript of Function overloading in c++

Page 1: Function overloading in c++

Yogi’s Guide to

C++Yogi’s Guide

toC++

Function Overloading

Yogendra Pal

Page 2: Function overloading in c++

www.learnbywatch.com | [email protected]

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.

Page 3: Function overloading in c++

www.learnbywatch.com | [email protected]

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.

Page 4: Function overloading in c++

www.learnbywatch.com | [email protected]

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

Page 5: Function overloading in c++

www.learnbywatch.com | [email protected]

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)

Page 6: Function overloading in c++

www.learnbywatch.com | [email protected]

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)

Page 7: Function overloading in c++

Yogi’s Guide to

C++Yogi’s Guide

toC++

Ask your questionsto learn better

Yogendra Pal

www.learnbywatch.com | [email protected]