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
Comments
Leave a comment