I am trying to finish a program that calculates a simple multiplication using addition and recursivity.
I.e 3 *4 = 3+3+3+3
So I have made a class as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class Multiplication
{
private:
int product;
public:
Multiplication() //constructor
{
product=0;
}
int CalculateMultip(int a, int b)
{
if(b!=0)
{
product=a+CalculateMultip(a,b-1); //implements recursivity
}
return product;
}
};
Now the main() program can compile if I take away the new Multiplication() part of the following snippet of code :
Multiplication objMultip=new Multiplication();
but when I do, I can only run the program once, because the value of "product" accumulates and if I run the Multiplication subprogram again, it shows the accumulated product, and this is not what I want. Is there any way I can reset it using the "new" function? Because at the moment the error message states:
error: conversion from 'Multiplication*' to non-scalar type 'Multiplication' requested
Is there any way I can reset it using the "new" function?
"new" will return a pointer to an instance. i.e. it will give you a Multiplication* which you would have to delete when you're done. This is dynamic memory allocation and this is not necessary for your current problem.
I would suggest to write CalculateMultipwithout the product variable as it is not necessary.
How can I write CalculateMultip without the product
variable? Without it the function wouldn't use recursion, which is what I have been asked to use :S
I've tried changing the line from: Multiplication objMultip=new Multiplication();
to: Multiplication* objMultip=new Multiplication();
and even: Multiplication* objMultip=new Multiplication;
but the same error appears, on line 5 of the main() program below,
where I have a case that says:
1 2 3 4 5 6
case 2: cout << "Write the first number: ";
cin>>pA;
cout << "Write the second number you would like to multiply: ";
cin>>pB;
cout << pA<<" * "<<pB<<" = "<<objMultip.CalculateMultip(pA,pB)<<endl;
break;
here the error message is:
error: request for member 'CalculateMultip' in 'objMultip', which is of non-class type 'Multiplication*'
You don't have to use dynamic allocation, you can just create a local instance inside a loop:
1 2 3 4 5 6 7 8 9
while (loop == true)
{
Multiplication theMultip; // Constructs with default constructor (no params)
cin >> pA;
cin >> pB;
cout << theMultip.CalculateMultip(pA, pB);
}
That way, it creates a Multiplication object every time round the loop.
Alternately, if you don't want to keep creating new objects, you can provide a reset() function in the Multiplication class and call that to clear the results out.
That works perfectly Jim, thanks to all that replied, with the loop it resets just as I want it to! =)
1 2 3 4 5 6 7 8 9 10 11 12 13
case 2:
loop=true;
while (loop == true)
{
Multiplication objMultip; // Constructs with default constructor (no params)
cout << "Write the first number: ";
cin>>pA;
cout << "Write the second number you would like to multiply: ";
cin>>pB;
cout << pA<<" * "<<pB<<" = "<<objMultip.CalculateMultip(pA,pB)<<endl;
loop=false;
}
break;
How can I write CalculateMultip without the product variable? Without it the function wouldn't use recursion, which is what I have been asked to use :S
This function uses recursion and does not use the product variable.
1 2 3 4 5 6 7 8 9 10 11
int CalculateMultip(int a, int b)
{
if(b == 0)
{
return 0;
}
else
{
return a + CalculateMultip(a,b-1); //implements recursivity
}
}
Just one more - now I see how you're using it, you don't need the loop within the case 2: section; you're always setting loop=false after the first iteration, so it can become:
1 2 3 4 5 6 7 8
case 2: {
Multiplication objMultip;
// Do prompting/reading
cout << pA<<" * "<<pB<<" = "<<objMultip.CalculateMultip(pA,pB)<<endl;
}
break;
On a related note - this example is fine because the Multiplication class is lightweight and doesn't have much overhead to construct/destruct it.
If it wasn't lightweight (it contained a lot of members that all needed initialising, for example), and you were running around the main loop many many times, it would become a performance issue because it would take time to construct/destruct each instance. In that case you'd be better off just re-using the same instance and resetting it.