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

O

ofranko

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;
}
 
J

jason.cipriani

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
 
O

osmium

exil said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top