C++ Operator Overloading Add the requested functionality to the Distance class b
ID: 3684462 • Letter: C
Question
C++ Operator Overloading
Add the requested functionality to the Distance class below:
class Distance {
private:
int feet;
int inches;
public:
Distance(int inch) { feet = inch/12; inches = inch % 12; };
Distance(int f, int i) { feet=f; inches=i; };
//Overloaded + that will add two distance objects and return sum in a
//distance object
//Conversion function (to type int) that will return the distance in inches
//Overloaded pre-increment operator that adds 1 to inches
//Overloaded post-increment operator that adds 1 to inches
}; //end Distance
Explanation / Answer
class Distance {
private:
int feet;
int inches;
public:
Distance(int inch) { feet = inch/12; inches = inch % 12; };
Distance(int f, int i) { feet=f; inches=i; };
//Overloaded + that will add two distance objects and return sum in a
//distance object
Distance operator+(const Complex& distance2);
//Conversion function (to type int) that will return the distance in inches
int convert(int f1,int i1)
{
int i2=f1*12;
int total=i2+i1;
return total;
}
//Overloaded pre-increment operator that adds 1 to inches
Distance operator++()
{
inches++;
return *this;
}
//Overloaded post-increment operator that adds 1 to inches
Distance operator ++(Distance &dis)
{
++inches;
dis.inches=inches;
return dis;
}
}; //end Distance
distance distance :: operator + (distance d2)
{
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 distance (f, i); // return a temporary distance
// initialized to sum
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.