Thread: Overloaded Functions

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Overloaded Functions

    I have been tasked with an assignment for "oddball questions". Basically it is a case-switch statement that should handle several different situations.

    The assignment is very simple, and I will post the full assignment as an attachment. However, (from my TA):

    Your program must have the following functions:

    a getName function which will input the name of the Geek
    an isEven function that takes two integers and returns a boolean value indicating if the sum of the numbers is even or not
    a sum function that takes two integers and computes and returns an int which is the sum of all numbers between the two inclusive (include the numbers in the sum) – for full credit this method should work even if the two numbers are the same (the sum is just one of the numbers) or if the first number is larger than the second. Also, you cannot assume which number will be greater.
    a isLeapYear function that takes an integer and returns a boolean value indicating if the number is a leap year. A leap year is one with 366 days. A year is a leap year if it is divisible by 4 (for example, 1980), and if it is not divisible by 100 (for example 1900); however, it is a leap year if it is divisible by 400 (for example, 2000).
    a display function which will display the geeks name and the number of questions that have been asked so far. This question is not included in calculating the number of questions asked so far.
    The four functions should have the following prototypes:

    string getName(int & countQuestions);
    boolean isEven(int firstNumber, int secondNumber, int & countQuestions);
    integer isEven(int firstNumber, int secondNumber, int & countQuestions);
    boolean isLeapYear(int year, int & countQuestions);
    void display(string name, int countQuestions);
    The program will ask a user to enter one of the following commands. Based on the user's choice, the program needs to perform corresponding operation. The program will terminate when the user enters 'q'.

    Now, I don't think that the function prototypes are possible as they differ only from the type of return.
    When following my assignment sheet, I receive many errors from visual C++ regarding the fact that the two prototypes differ only by the type of return.
    Any insight would be greatly appreciated.

    q.

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    [i didn't read the 2nd paragraph of ur post]

    bool and ints are technically the same thing in C++
    What's int isEven() supposed to return anyways?

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Now, I don't think that the function prototypes are possible as they differ only from the type of return.
    That is right. Check with your teacher as it is probably a mistake. I suspect that the version that returns a bool is the correct one, since by the name "isEven" I would expect the function to return true if the sum of the numbers is even. A non-zero value can also be interpreted as true, but in the context of C++ that would be unnecessary here.

    bool and ints are technically the same thing in C++
    Not quite the same, of course. Certainly not technically.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    from my code:

    Code:
    int isEven(int iEven1, int iEven2, int& questionCount)
    {
    	int evenSum;
    
    	cout << "Please enter two integers, separated by a space: ";
    	cin >> iEven1 >> iEven2;
    	evenSum = iEven1 + iEven2;
    	questionCount++;
    	if (evenSum % 2 == 0)
    		return 1;
    	else
    		return 0;
    }
    my bool isEven function is essentially the same thing, where true replaces 1, and false replaces 0.

    Code:
    bool isEven(int bEven1, int bEven2, int& questionCount)
    {
    	int evenSum;
    
    	cout << "Please enter two integers, separated by a space: ";
    	cin >> bEven1 >> bEven2;
    	evenSum = bEven1 + bEven2;
    	questionCount++;
    	if (evenSum % 2 == 0)
    		return true;
    	else 
    		return false;
    }
    Basically, I'm not too worried about the code, as I have ~85% working already and the rest is just me running into this issue. What I'm looking for is the coexistance of bool functions and int functions where the arguments are the same.
    I know that this should not work; I'd have to change one to only accept a single integer or maybe a double or something of those lines, however this is what has handed to me by my TA.
    My mind says this shouldn't work BECAUSE the function prototypes HAVE to be as shown:
    Code:
    string getName(int & questionCount); 
    bool isEven(int bEven1, int bEven2, int & questionCount); 
    int isEven(int iEven1, int iEven2, int & questionCount); 
    int sum(int sum1, int sum2, int & questionCount);
    bool isLeapYear(int yearLeap, int & questionCount); 
    void display(string geekName, int & questionCount);
    and because they are the same, the main function can't differentiate between the two.

    Let me know if this can/cannot be done. See the full code and assignment (in a poorly formatted text file, as it was originally given as an HTML file) in my previous post.

    q.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    my bool isEven function is essentially the same thing, where true replaces 1, and false replaces 0.
    Yes, I would expect that to be the case.

    I know that this should not work; I'd have to change one to only accept a single integer or maybe a double or something of those lines, however this is what has handed to me by my TA.
    My mind says this shouldn't work BECAUSE the function prototypes HAVE to be as shown:
    You are correct. However, I note that your TA wrote: "The four functions should have the following prototypes", but then went on to provide five possible prototypes. boolean and integer are not native types (they should be bool and int instead), so I think that your TA was just giving possible examples. Ultimately, you have to pick four of them and use the correct return types.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Ok. That confirms what I thought. Other than that, my code works well (aside from the quit case and the default case which I haven't put in yet).
    I've posted something along those lines on my class board.
    Thanks much.

    q.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  4. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM