/* This a more practical example of a new data type in C++ MONEY! */ #include class Money{ // This is *actually* a special case: // Dollars and cents // Optimized for computation rather than display long int d; short int c; // Invariant: -99<=c<=99, sign(d)==sign(c). // Model: money=d+c*0.01 (decimal) public: //Initalizations and casts. Money() :d(0),c(0){ } //clean initial values Money(const int n) :d(n), c(0){ } //conversion(cast) Money(const int d0, int c0) { d=d0 ; c=(d<0?-c0:c0); } Money(const double n) { d=(int)n ; c=100*(int(n)-d); } //a method for outputing Money. friend ostream& operator<<(ostream& s, Money n) { if(n.d>=0) { s<<" $" << (n.d)<<"." << (n.c<10?"0":"") <0 ) return Money(d * b+cc/100, cc%100) ; else return Money(d * b+cc/100, ((-cc)%100) ) ; } Money operator-() { return Money( - d, -c ); } friend Money operator-(Money a, Money b) { return a + (- b) ; } //Overload the comparison operators friend int operator<(Money a, Money b) { if (a.d < b.d) return 1; else if (a.d == b.d) return a.c(Money a, Money b) { return b < a ; } friend int operator>=(Money a, Money b) { if (a.d >= b.d) return 1; else if (a.d == b.d) return a.c>=b.c ; else return 0; } friend int operator<=(Money a, Money b) { return b >= a ; } friend int operator==(Money a, Money b) { return a.d == b.d && a.c == b.c; } friend int operator!=(Money a, Money b) { return !( a == b); } int dollars(); int cents(); }; // end class Money //Sample of placing definition outside class inline int Money::dollars(){return d;} inline int Money::cents(){return c;} // inline implies direct substitution of code, not function call(faster) main() { Money a,b,c; int i; a=Money(4,20); b=Money(-4,20); c=Money(100); cout <<"a="<< a << " "; cout <<"b="<< b << " "; cout<<"c = "<< c<< "\n"; if( a