Function overload

Help with my code

Page 1 of 1

7 Replies - 551 Views - Last Post: 23 September 2008 - 01:07 AM Rate Topic: -----

#1 killakev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-September 08

Function overload

Posted 22 September 2008 - 05:44 PM

Im writing a code to demonstrate the overload functions and I need help on doing so here is what I have right now...
#include <iostream>
using namespace std;

int min(int x; int y);
int min(int x; int y; int z);

int main()
{
	int x=1;
	int y=2;
				int z= 3;
	return 0;
}

int min (int x; int y )
{

	if (x>y)
	{
	return (x>y);
	}
	else if
	{
		return (y>x);
	}
}

int min (int x; int y; int z)
{
	if ( x> y && x>z && y>z )
	{
		return (x>y>z);
	}
	else if (y>x && y>z && x>z)
	{
		return (y>x>z);
	}
	else if (z>x && z>y && x>y)
	{
		return (z>x>y);
	}
	else if (y>x && y>z && z>x)
	{
		return (y>z>x);
	}
	else if (x>y && x>z && z>y)
	{
		return (x>z>y);
	}
	else if (z>x && z>y && y>x)
	{
		return (z>y>x);
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Function overload

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Function overload

Posted 22 September 2008 - 05:48 PM

what's the question?
Was This Post Helpful? 0
  • +
  • -

#3 killakev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-September 08

Re: Function overload

Posted 22 September 2008 - 06:21 PM

i guess just getting the code to work because i cant get it to work for some reason Im suppose out put the minimum of two numbers and than output the minimum of three numbers
Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 205
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Function overload

Posted 22 September 2008 - 06:39 PM

your main function never calls min, so there will not be an output.

The idea with overloading functions is that (for example) both min(1,2); and min(1,2,3); are appropriate entries here, since you have defined it as a function accepting 2 or 3 values.

I believe if you make your main method:
int main()
{
    int x=1;
    int y=2;
    int z= 3;

    cout<<min(x,y);
    cout<<min(x,y,z);

    return 0;
}



it will work more closely to what you are expecting.

This post has been edited by William_Wilson: 22 September 2008 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,119
  • Joined: 22-February 08

Re: Function overload

Posted 22 September 2008 - 08:00 PM

I'm not sure what William_Wilson is talking about, I'm still a bit dizzy. just woke up from my sleep :D
edited : now I understand it. your main function never calls the min function. but your code should compiles just fine even if the function is never used.

but there's one thing that I catch. your function prototype should be :
int min(int x, int y);
int min(int x, int y, int z);


use comma, not semicolon. after the main function, both your min function should also use comma, not semicolon.

hope that helps you :)

This post has been edited by red_4900: 22 September 2008 - 08:05 PM

Was This Post Helpful? 0
  • +
  • -

#6 killakev  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-September 08

Re: Function overload

Posted 22 September 2008 - 09:46 PM

View Postred_4900, on 22 Sep, 2008 - 08:00 PM, said:

I'm not sure what William_Wilson is talking about, I'm still a bit dizzy. just woke up from my sleep :D
edited : now I understand it. your main function never calls the min function. but your code should compiles just fine even if the function is never used.

but there's one thing that I catch. your function prototype should be :
int min(int x, int y);
int min(int x, int y, int z);


use comma, not semicolon. after the main function, both your min function should also use comma, not semicolon.

hope that helps you :)



Yes that was very helpful! Thank you both but I still cant figure out why I still get syntax errors...
#include <iostream>
using namespace std;

int min(int x, int y);
int min(int x, int y, int z);

int main()
{
	int x=1;
	int y=2;
	int z=3;
	cout<<min(x,y);
	cout<<min(x,y,z);
	return 0;
}

int min (int x, int y )
{
	if (x>=y)
	{
	return (x>y);
	}
	else if
	{
		return (y>x);
	}
}

int min (int x, int y, int z)
{
	if ( x>=y && x>=z && y>=z )
	{
		return (x>y>z);
	}
	else if (y>=x && y>=z && x>=z)
	{
		return (y>x>z);
	}
	else if (z>=x && z>=y && x>=y)
	{
		return (z>x>y);
	}
	else if (y>=x && y>=z && z>=x)
	{
		return (y>z>x);
	}
	else if (x>=y && x>=z && z>=y)
	{
		return (x>z>y);
	}
	else if (z>=x && z>=y && y>=x)
	{
		return (z>y>x);
	}
}


Was This Post Helpful? 0
  • +
  • -

#7 red_4900  Icon User is offline

  • Code T(h)inkers
  • member icon

Reputation: 21
  • View blog
  • Posts: 1,119
  • Joined: 22-February 08

Re: Function overload

Posted 22 September 2008 - 10:55 PM

in your min function, you didn't put any statement there else if. there should be a statement like else if(some statement here). or you could just use else condition.
int min (int x, int y )
{
    if (x>=y)
    {
    return (x>y);
    }
    else
    {
        return (y>x);
    }
}


hope that helps you :)
Was This Post Helpful? 0
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Function overload

Posted 23 September 2008 - 01:07 AM

To expand on red's point, you could use ternary operators in your min function, and do it all in one line:
#include <iostream>
using namespace std;

float min (float x, float y)
{
    return (x < y ? x : y);
}

int main ()
{
    cout << min (4.5f, 5.11f) << endl
         << min (58.2f, 3.222f);
    return EXIT_SUCCESS; // the program ran successfully! w00t!
}


:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1