Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <iostream>
using namespace std;
class Distance {
int feet;
int inches;
public:
Distance(): feet(0), inches(0){}
Distance(int f, int i): feet(f), inches(i){
if (feet < 0 && inches < 0) {
inches = 0 - inches;
}
}
void displayDistance () {
cout << feet << "\' " << inches << "\"" << endl;
}
bool operator < (Distance const& d) {
int m = (feet * 12) + inches;
int n = (d.feet * 12) + d.inches;
return (m < n);
}
Distance operator + (Distance const& d) {
int dist = ((feet * 12) + inches) + ((d.feet * 12) + d.inches);
Distance nd(dist / 12, dist % 12);
return nd;
}
Distance operator - (Distance const& d) {
int dist = ((feet * 12) + inches) - ((d.feet * 12) + d.inches);
Distance nd(dist / 12, dist % 12);
return nd;
}
Distance operator ++ () {
if(++inches > 11) {
inches = 0;
++feet;
}
return *this;
}
};
int main (void) {
Distance D1(2, 6), D2(3, 6);
D1.displayDistance();
cout << "+" << endl;
D2.displayDistance();
cout << "=" << endl;
Distance D3 = D1 + D2;
D3.displayDistance();
cout << endl << endl << "++";
D1.displayDistance();
//++D1;
D1.displayDistance();
cout << endl << endl;
D1.displayDistance();
cout << "-" << endl;
D2.displayDistance();
cout << "=" << endl;
Distance D5 = D1 - D2;
D5.displayDistance();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment