6.5 Overloading Binary Operators
6.5 Overloading Binary Operators
dist3.add_dist(dist1, dist2);
By overloading the + operator we can reduce this dense-looking expression to
dist3 = dist1 + dist2;
Here‘s the listing for ENGLPLUS, which does just that:
// englplus.cpp
// overloaded ‗+‘ operator adds two Distances
#include <iostream.h>
class Distance //English Distance class
{
private:
int feet;
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0)
{ } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in)
{}
void getdist() //get length from user
{
cout << ―\nEnter feet: ―; cin >> feet;
cout << ―Enter inches: ―; cin >> inches;
}
void showdist() const //display distance
{ cout << feet << ―\‘-‖ << inches << ‗\‖‘; }
Distance operator + ( Distance ) const; //add 2 distances
};
//add this distance to d2
Distance Distance::operator + (Distance d2) const //return sum
{
int f = feet + d2.feet; //add the feet
float i = inches + d2.inches; //add the inches
if(i >= 12.0) //if total exceeds 12.0,
{ //then decrease inches
i -= 12.0; //by 12.0 and
f++; //increase feet by 1
} //return a temporary Distance
return Distance(f,i); //initialized to sum
}
int main()
{
Distance dist1, dist3, dist4; //define distances
dist1.getdist(); //get dist1 from user
Distance dist2(11, 6.25); //define, initialize dist2
dist3 = dist1 + dist2; //single ‗+‘ operator
dist4 = dist1 + dist2 + dist3; //multiple ‗+‘ operators
//display all lengths
cout << ―dist1 = ―; dist1.showdist(); cout << endl;
cout << ―dist2 = ―; dist2.showdist(); cout << endl;
cout << ―dist3 = ―; dist3.showdist(); cout << endl;
cout << ―dist4 = ―; dist4.showdist(); cout << endl;
return 0;
}
To show that the result of an addition can be used in another addition as
well as in an assignment, another addition is performed in main (). We
add dist1, dist2, and dist3 to obtain dist4 (which should be double the
value of dist3), in the statement
dist4 = dist1 + dist2 + dist3;//Nameless Temporary Object will hold the intermediate
result from adding dist1 and dist2.
Here‘s the key: The argument on the left side of the operator (dist1 in
this case) is the object of which the operator is a member. The object on
the right side of the operator (dist2) must be furnished as an argument
to the operator. The operator returns a value, which can be assigned or
used in other ways; in this case it is assigned to dist3. Figure 2 shows
how this looks.
FIGURE 2
This is similar to the arrangement used in COUNTPP3, except that the constructor
takes two arguments instead of one. The statement
dist1 += dist2;
Exercise
1. To the Distance class in the ENGLPLUS program in this chapter, add an
overloaded - operator that subtracts two distances. It should allow statements like
dist3= dist1-dist2;. Assume that the operator will never be used to subtract a larger
number from a smaller one (that is, negative distances are not allowed).
soluation
1).
// ex8_1.cpp
// overloaded ‗-‘ operator subtracts two Distances
#include <iostream.h>
class Distance //English Distance class
{
private:
int feet;
float inches;
public: //constructor (no args)
Distance() : feet(0), inches(0.0)
{ } //constructor (two args)
Distance(int ft, float in) : feet(ft), inches(in) { }
void getdist() //get length from user
{
cout << ―\nEnter feet: ―; cin >> feet;
cout << ―Enter inches: ―; cin >> inches;
}
void showdist() //display distance
{ cout << feet << ―\‘-‖ << inches << ‗\‖‘; }
Distance operator + ( Distance ); //add two distances
Distance operator - ( Distance ); //subtract two distances
};
//add d2 to this distance
Distance Distance::operator + (Distance d2) //return the sum
{
int f = feet + d2.feet; //add the feet
float i = inches + d2.inches; //add the inches
if(i >= 12.0) //if total exceeds 12.0,
{ //then decrease inches
i -= 12.0; //by 12.0 and
f++; //increase feet by 1
} //return a temporary Distance
return Distance(f,i); //initialized to sum
}
//subtract d2 from this dist
Distance Distance::operator - (Distance d2) //return the diff
{
int f = feet - d2.feet; //subtract the feet
float i = inches - d2.inches; //subtract the inches
if(i < 0) //if inches less than 0,
{ //then increase inches
i += 12.0; //by 12.0 and
f--; //decrease feet by 1
} //return a temporary Distance
return Distance(f,i); //initialized to difference
}
int main()
{
Distance dist1, dist3; //define distances
dist1.getdist(); //get dist1 from user
Distance dist2(3, 6.25); //define, initialize dist2
dist3 = dist1 - dist2; //subtract
//display all lengths
cout << ―\ndist1 = ―; dist1.showdist();
cout << ―\ndist2 = ―; dist2.showdist();
cout << ―\ndist3 = ―; dist3.showdist();
cout << endl;
return 0; }