Function having many forms – CSKC
Sign In

Search

Code Learning #CPP#4646    siteicon   siteicon  

Function having many forms

Demonstration of a kind of polymorphism with multiple functions have different parameters.

Learning Objectives

  • Understanding how function overloading is a type of polymorphic behaviour.

Source Code

TC++ #4646

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

void printdouble(int x) { cout<<2*x<<endl; }
This function simply prints the double value of integer parameter passed to it.

void printdouble(char c) {cout<<c<<c<<endl;}
This function print the passed character twice one after another.

int main(){
int a=12; char ch=’A’;
Appropriate variables are declared.

cout<<“Double of “<<a<<” is “; printdouble(a);
Integer doubling variation of overloaded function is used as passed parameter is an integer.

cout<<“Double of “<<ch<<” is “;  printdouble(ch);
Character printing one after the another variation of overloaded function is used as passed parameter is a character.

return 0;}

Notes

  • Functions overloading may not be considered as type of polymorphism by purists as compiler itself identifies two different functions and which reference is to be called is decided at the compile time only. Some other experts have called it adhoc polymorphism.

Common Errors

  • Some time programmers try to define overloading with just different return data types. For function overloading function signature (parameter data type, parameter count, parameter order) must be different.


Suggested Filename(s): func-ol.cpp,function-overloading.cpp



Share

Created: 6-Sep-2018 20:38 | Updated: 6-Sep-2018 20:40|