Skip to content

Instantly share code, notes, and snippets.

@fa7ad
Last active April 8, 2018 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fa7ad/2c6de14a5c39e7ae924b8afa0668d319 to your computer and use it in GitHub Desktop.
Save fa7ad/2c6de14a5c39e7ae924b8afa0668d319 to your computer and use it in GitHub Desktop.
#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