/* There's something funny about this program (not that there are any errors), which concerns the nature of OOP in C++/Java/C#. In particular, pay attention to the usage of "public" and "private". Don't take everything about these languages for granted! Think about possible alternatives. */ #include #define BOOLEAN int class fraction { private: int numerator; int denominator; int gcd(int a, int b) { if (a==0) return b; else return gcd(b%a,a); } // euclid's alg public: fraction(int n, int d) // constructor { if (d==0) d = 1; int cd = gcd(n,d); // common divisor numerator = n/cd; denominator = d/cd; } ~fraction() {} // trivial destructor // check if one fraction is equal to another fraction: BOOLEAN equals(fraction *f) { return numerator*f->denominator == denominator*f->numerator; } // multiply fractions fraction* mult(fraction *f) { return new fraction(numerator*f->numerator,denominator*f->denominator); } }; // end of class fraction int main(int argc, char **argv) { fraction *f1, *f2, *f3; f1 = new fraction(1,3); f2 = new fraction(2,5); f3 = f1->mult(f2); if (f3->equals(new fraction(2,15))) cout << "nice program!\n"; return 0; }