Function Overloading... Help !!! | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

Function Overloading... Help !!!

https://code.sololearn.com/cLQwChQS728M/?ref=app I have made program (in above link) of function overloading in which function with "integer" argument calculates square and function with "float" integer calculates cube of the number. but in output it shows square of the number for both function... i know there is something wrong in "calling" of function in second case... as it is calling only square function please help with this code... what are the modifications needed.. thank you

24th Jun 2017, 1:19 PM
Subhankar Golder
Subhankar Golder - avatar
8 Respostas
+ 3
@Elie It was really helpful. Thank you šŸ˜‡
24th Jun 2017, 1:53 PM
Subhankar Golder
Subhankar Golder - avatar
+ 2
#include<iostream> #include<conio.h> using namespace std; int calc_x(int); int calc_y(float); int calc_x(int x){ return x*x; } int calc_y(float y){ return y*y*y; } int main(){ int a,b; cout<<"enter the number-->"; cin>>a; cout<<"\n the square of "<<a<<" is "<<calc_x(a)<<endl; cout<<"\n enter the another number for finding its cube"<<endl; cin>>b; cout<<"cube of "<<b<< " is "<<calc_y(b); getch(); //Better Trying it in an C++ compiler or visual studio return 0; }
24th Jun 2017, 1:25 PM
Elie Douaihy
Elie Douaihy - avatar
+ 2
@Elie Its a function overloading program.. Functions name has to be same..
24th Jun 2017, 1:28 PM
Subhankar Golder
Subhankar Golder - avatar
+ 2
in function or you can say method:- ------------------------------------------------------- 1) same name but different (data type) return type void fun() { //logics here } int fun() { //logics here } 2)same data type but different argument void f1(int a) { //logics here } void f1(int a, int b) { //logics here }
24th Jun 2017, 1:41 PM
meherDev
meherDev - avatar
+ 2
#include<iostream> #include<conio.h> using namespace std; int calc(int); int calc(float); class Base{ public: int calc(int x){ return x*x; } }; class Base2: public Base { public: int calc(float b){ return b*b*b; } }; int main(){ int a,b; cout<<"enter the number-->"; cin>>a; Base obj; cout<<"\n the square of "<<a<<" is "<<obj.calc(a)<<endl; cout<<"\n enter the another number for finding its cube"<<endl; cin>>b; Base2 obj1; cout<<"cube of "<<b<< " is "<<obj1.calc(b); getch(); //Better Trying it in an C++ compiler or visual studio return 0; } thats it,sorry havent seen Override
24th Jun 2017, 1:41 PM
Elie Douaihy
Elie Douaihy - avatar
+ 2
@Elie sir can it be done without class ?
24th Jun 2017, 1:49 PM
Subhankar Golder
Subhankar Golder - avatar
+ 2
By using same Fonction you need to use class Inherite...Or changing Function names
24th Jun 2017, 1:52 PM
Elie Douaihy
Elie Douaihy - avatar
0
@Subhankar Bhai, in c++,c u r able to apply overloading method(or function) without class. no problem at all. but in java it is not possible because java means class and its objects.
24th Jun 2017, 4:34 PM
meherDev
meherDev - avatar