Re: [Dev-C++] Begginer's question, Function overloading
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Brian P. <br...@su...> - 2002-05-02 15:41:21
|
No, well...no..um...o.k., in short I think what you want to do is
something like this:
void function(int, float);
This function prototype will take an int and double.
Now overload it:
void function(float, float, float);
Notice that both these functions are called 'function'. You must give
them the SAME name but different parameters. The compiler will know
which function to call depending on the parameters. If I call:
function(32,3.0);
the compiler will use the first function. However, if I call:
function(32.1,33.4.92.923);
the compiler will automatically call the second function. Isn't it
wonderfull?!
Got it?
You can specify different return types but in order to overload a
function you MUST give them the same name and you MUST give them
different parameters (different types and/or numbers).
Hope that helps.
b--
On Sat, 2015-04-18 at 11:06, Blagger wrote:
> Hi
>
> As a beginner I came to the chapter "function overloading" of a C++ handbook
> and had the following exercise.
> The problem with this listing is that dev-C++ gives me the errors :
> 110 too many arguments to function double f-seize(double,double)
> 320 at this point in line.
>
> I know this, that's why it's called function overloading, or am I completely
> wrong ?
> Thanks in advance for any help.
>
> 10 // computing clothes size
> 20 // excercise on functions with function overloading
> 30
> 40 #include <stdio.h>
> 50 #include <iostream.h>
> 60 #include <conio.h>
> 70 #include <math.h>
> 80
> 90 double hat_seize(double h_weight,double h_height);
> 100 // first function returns hat-seize
> 110 double f_seize(double j_weight,double j_years);
> 120 // second function returns Waist in inches
> 130 // when overloaded and returns also Jacket seize
> 140
> 150 int main(int argc, char *argv[])
> 160 {
> 170 cout.setf(ios::fixed);
> 180 cout.setf(ios::showpoint);
> 190 cout.precision(1);
> 200 char answer = 'y';
> 210 double height, weight, age, h_seize,j_seize,w_seize;
> 220
> 230 do
> 240 {
> 250 cout << "\nTyp hier uw lengte in (in cm) : "; cin >> height;
> 260 cout << "\nTyp hier uw gewicht in (in kg) : "; cin >> weight;
> 270 cout << "\nTyp hier uw leeftijd in (jaren) : "; cin >> age;
> 280 cout << "\nEffe wachtte" << endl;
> 290 h_seize = hat_seize(weight,height);
> 300 w_seize = f_seize(weight,age);
> 310 // now we overload the second function :
> 320 j_seize = f_seize(weight,height,age);
> 330 cout << "\n De grootte van uw hoed : "; cin >> h_seize;
> 340 cout << "\n De grootte van uw jas : "; cin >> j_seize;
> 350 cout << "\n De grootte van uw broek : "; cin >> w_seize; cout <<
> endl;
> 360 cout << "\n\tNogmaals? (Y/N) : "; cin >> answer;
> 370
> 380 }
> 390 while ((answer == 'Y') || (answer == 'y'));
> 400
> 410 getch();
> 420
> 430 return 0;
> 440 }
> 450
> 460 // first function calculates hat-size
> 470 double hat_seize(double h_weight,double h_height)
> 480 {
> 490 return ((h_weight / h_height) * 2.9);
> 500 }
> 510
> 520 //Second function (normal)
> 530 double f_seize(double j_weight,double j_years)
> 540 {
> 550 return ((j_weight/5.7) + (floor((j_years - 28)/2)/10));
> 560 }
> 570 //Second function (overloaded)
> 580 double f_seize(double j_weight,double j_height, double j_years)
> 590 {
> 600 return (((j_weight * j_height)/288) + (floor((j_years - 30)/10)/8));
> 610 }
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|