Question #150632

An overloaded operator always requires one less argument than its number of operands.
Why?

Expert's answer

An overloaded operation always requires one less number of arguments than the number

operands as one of the operands is an object calling the function


Distance Distance::operator+(Distance d2) const
{
int f = feet + d2.feet; // add feet
float i = inches + d2.inches; // add inches
if(i >= 12.0) // if there are more than 12 inches
{
i -= 12.0; // then we decrease inches by 12
f++; // and increase the feet by 1
}
return Distance(f, i); // create and return a temporary variable
}
///////////////////////////////////////////////////////////
int main()
{
Distance dist1, dist3, dist4; // define variables
dist1.getdist(); // get information
Distance dist2(11, 6.25); // define a variable with a specific value
dist3 = dist1 + dist2; // add two variables
dist4 = dist1 + dist2 + dist3; // adding several variables

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS