Reputation: 143
I'm writing a basic class program and I have it all done without operator overloading, and now I want to try overloading all my operators. I'm also going to try out +, ==, !=, and << but right now I'm confused on the multiplying.
In the test driver, the user inputs a factor. I have this factor defined in the class but don't know how to assign it a value from the driver. Any assistance?
In the operator overload function I want to multiply that factor by the object. --> There are five components to the object, which is a list of numbers. Sum, Length, Mean, Minimum, and Maximum. The point of multiplying an object by a constant is to produce a class that is identical to the first, except each number in the list has been multiplied by the factor. To reach this end, all that needs to be done is to multiply the Sum by the factor, divide the new Sum by the old Length to get the new Mean, and then multiply the Min and Max by the factor.
Here's my overload so far:
Statistician Statistician::operator* (Statistician& temp)
{
Statistician Stat3;
Stat3.newLength = temp.newLength;
Stat3.newSum = (factor * temp.newSum);
Stat3.newMean = (temp.newSum / temp.newLength);
Stat3.newMaximum = (factor * temp.newMaximum);
Stat3.newMinimum = (factor * temp.newMinimum);
return Stat3;
}
And here's what is in the test driver:
system("CLS");
cout << endl << " Multiply by factor of: ";
cin >> // Input factor
Stat3 = Stat1 * factor;
Finally, I have "factor" defined in the class, under "private" - let me know if that's wrong.
Upvotes: 1
Views: 230
Reputation: 56863
You usually don't want to modify the object that you are applying operator*
to, hence its signature would be
Statistician Statistician::operator* (const Statistician& temp) const;
where if you call Stat1*factor
, *this
is Stat1
and temp
is factor
. That said, you probably want to consider implementing operator*=
and define operator*
in terms of that. There are libraries to help you with that, see my profile for some links.
If you want to implement operator*=
, its signature will be
Statistician& Statistician::operator*= (const Statistician& value);
and, if factor
could be some other type, you can overload on that type, for example:
Statistician& Statistician::operator*= (const int value);
to allow scaling an instance of Statistician
by an integer value. The latter seems more appropriate than storing some factor inside your class, but without seeing more of your code it's hard to tell.
Here's a suggestion for an operator*=(const int factor)
:
Statistician& Statistician::operator*= (const int factor)
{
newSum *= factor;
newMean = newSum / newLength;
newMaximum *= factor;
newMinimum *= factor;
return *this;
}
as you can see, things usually become easier and hence less error-prone if you do them the right way.
Now that you can call operator*=
with a factor directly, there is no need to store a factor in your class and the class itself becomes easier. Finally, you can define operator*
as a free function outside of your class:
Statistician operator* (const Statistician& lhs, const int factor )
{
Statistician result( lhs ); // make a copy
result *= factor;
return result;
}
or just use one of the libraries to generate operator*
for you.
Upvotes: 1