class (I) - C Classes We want to implement a program to...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 C++ Classes We want to implement a program to support the processing of fraction , f = ±²³´µ¶·¸µ ¹´±¸³º±¶·¸µ Requirements of the representation ( representation invariants ) - numerator and denominator are integers - denominator > 0 - numerator and denominator are relatively prime, e.g. » ¼ is reduced to ½ ¾ - only 1 representation of the value zero, ¿ À Modeling of fraction using C++ class #include <ostream> // helper function int gcd(int m, int n) //compute gcd of two +ve integers { int r; while ((r = m % n) > 0) { m = n; n = r; } return n; }
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 class fraction { //overload the operator<< such that you can output //a fraction object to an output stream, //e.g. cout << f //The purpose of this function is similar to the method //toString() in Java. // A non-member function can access the private and // protected members of a class if it is declared as // a friend of that class. friend ostream& operator<<(ostream& os, fraction& f); private: int numerator; int denominator; public: fraction() //default constructor { numerator = 0; denominator = 1; } fraction(int n, int d) // ensure conformant with the { // representation invariants if (d == 0) { cerr << “ERROR: denominator is zero.” << endl; exit(0); } if (n == 0) { numerator = 0; denominator = 1; return; } if (d < 0) { n *= -1; d *= -1; } int g; if (n < 0)
Image of page 2
3 g = gcd(-n, d); else g = gcd(n, d); numerator = n / g; denominator = d / g; } //overload (redefine) the operators //keyword ‘const’ makes the reference ‘other’ immutable fraction operator+
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern