first time trying to overload a method/member Fx --need help

Discussion in 'C++' started by ofranko, Jan 4, 2009.

  1. ofranko

    ofranko Guest

    i'm trying to overload fx "myAnswer"... i want to return - int
    myAnswer when
    myAnswer== sumOf ..... when they are != i want the message
    "Incorrect..." to be returned..
    i've included some of the code ..if you need more just ask..



    thank you for your help,
    frank

    i'll continue working on it..if i get i'll let you know

    class AddingNumbers
    {
    public:
    AddingNumbers(int initialRandomNumber, int initialAddTable,
    int initialsumOf, int initialMyAnswer);
    ~AddingNumbers();
    int GetRandomNumber();
    void SetRandomNumber(int randomNumber);
    int GetAddTable() ;
    void SetAddTable(int addTable);
    int GetsumOf();
    void SetsumOf(int sumOf);
    int GetMyAnswer();
    //string GetMyAnswer();
    void SetMyAnswer(int myAnswer);

    private:
    int theRandomNumber;
    int theAddTable;
    int theSumOf;
    int theMyAnswer;
    };

    string AddingNumbers::GetMyAnswer()
    {
    myAnswer ="Incorrect, Try Again ";
    cout << myAnswer<<endl;
    }
    int AddingNumbers::GetMyAnswer()
    {
    int myAnswer = 0;

    cout << "What is the Sum of " << theAddTable <<" + " <<
    theRandomNumber << " ?" <<'\n';
    cin >> myAnswer;//system("pause");
    if (myAnswer == theSumOf)
    {
    cout << "CORRECT!," <<'\n'<< "Next"<<'\n'<<'\n';
    cout << theAddTable << " + " << theRandomNumber << " = " <<
    theSumOf << '\n';
    //countedCorrect ++;
    return myAnswer;
    }
    else
    {
    cout <<" Incorrect," <<'\n' << "Next " <<'\n'<<'\n';
    cout << theAddTable << " + " << theRandomNumber << " = "<<
    theSumOf << '\n';
    }
    return myAnswer;
    }
    ofranko, Jan 4, 2009
    #1
    1. Advertising

  2. ofranko

    Guest

    , Jan 4, 2009
    #2
    1. Advertising

  3. ofranko

    Guest

    On Jan 4, 1:18 pm, ofranko <> wrote:
    > i'm trying to overload fx "myAnswer"... i want to return - int
    > myAnswer when
    >  myAnswer== sumOf ..... when they are != i want the message
    > "Incorrect..." to be returned..
    > i've included some of the code ..if you need more just ask..



    Joe Cook answered why; a function can only have one return type.

    One thing you could do is pass an int by reference to GetMyAnswer(),
    and have it return a bool indicating success, passing the answer back
    through the int on success, e.g.:


    // Returns true if correct, false if incorrect.
    // If correct, also stores answer in 'answer' parameter.
    bool AddingNumber::GetMyAnswer (int &answer) {

    ...

    if (myAnswer == theSumOf) {
    answer = myAnswer;
    return true;
    } else {
    return false;
    }

    }


    Then the usage might be:


    int answer;
    if (myAddingNumbers.GetMyAnswer(answer) == true) {
    cout << "Answer " << answer << " is correct!" << endl;
    } else {
    cout << "Your answer was incorrect." << endl;
    }


    There are quite a few ways you could change your design to make it do
    what you want, this is just one way.

    HTH,
    Jason
    , Jan 4, 2009
    #3
  4. ofranko

    osmium Guest

    "exil" wrote:

    >> On Jan 4, 1:18 pm, ofranko <> wrote:
    >>> i'm trying to overload fx "myAnswer"... i want to return - int
    >>> myAnswer when
    >>> myAnswer== sumOf ..... when they are != i want the message
    >>> "Incorrect..." to be returned..

    >> <snip>
    >>
    >> See
    >> http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-39.9
    >>
    >> Joe Cook

    >
    >
    > Is there no chance to implement this problem?


    The question asked by ofranko has been answered, it is not possible to
    overload by return type in the C++ language.

    If you have a new question, this would be a good place to ask it.
    osmium, Jan 7, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Piotre Ugrumov
    Replies:
    3
    Views:
    361
    Nick Hounsome
    Jan 25, 2004
  2. winbatch
    Replies:
    5
    Views:
    408
    Victor Bazarov
    Apr 25, 2005
  3. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    234
    Sherm Pendley
    Oct 11, 2004
  4. W Karas
    Replies:
    3
    Views:
    245
    Victor Bazarov
    Nov 30, 2012
  5. somenath
    Replies:
    10
    Views:
    258
    James Kanze
    Jul 2, 2013
Loading...

Share This Page