In: Computer Science
The class Distance is defined below:
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)
{ }
};
Overloading the following operators:
a. + to add two Distance objects using member function.
b. - to subtract two Distance objects using friend function
c. << and >>
Use the following main function to test your class Distance.
int main()
{
Distance dist1, dist3, dist4; //define distances
cin>>dist1;
Distance dist2(11, 6.25); //define, initialize dist2
dist3 = dist1 + dist2; //single '+' operator
dist4 = dist1 - dist2; //friend '-' operators
//display all lengths
cout << "dist1 = ";
cout<< dist1 << endl;
cout << "dist2 = ";
cout<< dist2 << endl;
cout << "dist3 = ";
cout<< dist3 << endl;
cout << "dist4 = ";
cout<< dist4 << endl;
return 0;
}
PLEASE DO THIS WITH C++
Also, using declaration of array and using [] notation for accessing element of array
#include <iostream>
#include <iomanip>
#include <bits/stdc++.h>
using namespace std;
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)
{
}
// Define stream insertion operator for Distances.
friend ostream& operator<<(ostream& str, Distance& d);
// Define stream extraction operator for Distances.
friend istream& operator>>(istream& str, Distance& d);
Distance operator-(Distance& rhs);
Distance operator+(Distance& rhs);
};
// Define + that operates on Distance + Distance.
Distance Distance::operator+(Distance& rhs)
{
int sum_feet;
double sum_inches;
sum_feet = feet + rhs.feet;
sum_inches = inches + rhs.inches;
if (sum_inches >= 12.0)
{
sum_inches -= 12.0;
++sum_feet;
}
Distance d(sum_feet, sum_inches);
return d;
}
// Define stream insertion operator for Distances.
ostream& operator<<(ostream& str, Distance&
d)
{
str << d.feet << "'" << d.inches <<
"\"";
return str;
}
// Define stream extraction operator for Distances.
istream& operator>>(istream& str, Distance&
d)
{
str >> d.feet;
if (str.get() != '\'')
{
cerr << "*** Error extracting Distance.\n";
exit(1);
}
str >> d.inches;
if (str.get() != '"')
{
cerr << "*** Error extracting Distance.\n";
exit(1);
}
return str;
}
// Define + that operates on Distance + Distance.
Distance Distance::operator-(Distance& rhs)
{
int diff_feet;
float diff_inches;
float sum = feet + inches;
float rsum = rhs.feet + rhs.inches;
float diff = abs(sum - rsum);
diff_feet = diff / 12;
diff_inches = diff - diff_feet;
Distance d(diff_feet, diff_inches);
return d;
}
int main()
{
Distance dist1, dist3, dist4; // define distances
cin >> dist1;
Distance dist2(11, 6.25); // define, initialize dist2
dist3 = dist1 + dist2; // single '+' operator
dist4 = dist1 - dist2; // friend '-' operators
// display all lengths
cout << "dist1 = ";
cout << dist1 << endl;
cout << "dist2 = ";
cout << dist2 << endl;
cout << "dist3 = ";
cout << dist3 << endl;
cout << "dist4 = ";
cout << dist4 << endl;
return 0;
}
========================================
========================================
Output:
========================================