C ++ Fraction class operator overload exercise

Source: Internet
Author: User

# Include <iostream> using namespace std; int GreatestCommonDivisor (int m, int n) // calculate the maximum approximate number of moving phase division {int r; do {r = m % n; m = n; n = r;} while (r! = 0); return m;} class Fraction; // ostream & operator <(ostream & out, const Fraction & fra ); // istream & operator> (istream & in, const Fraction & fra); // The three actions meet the compilation logic of vc6.0 and declare class Fraction {private: int fenzi, fenmu; // numerator, denominator public: Fraction (){}~ Fraction () {} Fraction (int fenzi, int fenmu); Fraction operator + (const Fraction & fra) const; Fraction operator-(const Fraction & fra) const; fraction operator * (const Fraction & fra) const; Fraction operator/(const Fraction & fra) const; friend ostream & operator <(ostream & out, const Fraction & fra ); // I/o overload must use the friend element overload friend istream & operator> (istream & in, const Fraction & fra); void setValue (int mu, int zi) ;}; Frac Tion: Fraction (int zi, int mu): fenzi (zi), fenmu (mu) {if (mu = 0) {cout <"the denominator cannot be 0! "<Endl; exit (0) ;}} ostream & operator <(ostream & out, const Fraction & fra) {if (fra. fenmu <0) out <"(-" <fra. fenzi <"/" <-fra. fenmu <")"; // The negative score shows else if (fra. fenzi <0) out <"(-" <-fra. fenzi <"/" <fra. fenmu <")"; else if (fra. fenmu = 1) out <fra. fenzi; // else out <fra. fenzi <"/" <fra. fenmu; // return out;} istream & operator> (istream & in, Fraction & fra) {int mu, zi; cout <"Enter denominator and Numerator" <endl; in> mu>> Zi; fra. setValue (mu, zi); return in;} void Fraction: setValue (int mu, int zi) {fenmu = mu; fenzi = zi;} Fraction :: operator + (const Fraction & fra) const {int a = fenzi, B = fenmu; // ▲▲▲ important ▲▲/// be sure to use const, if this parameter is not used, the data of the previous operation will be assigned to the next operation of l-value. // because the const function cannot change the internal variables of the class, you must use a replacement, if you use the friend overload here, // can effectively avoid this problem, because the friend overload operator has two referenced parameters a = fra. fenmu * fenzi + fra. fenzi * fenmu; B * = fra. fenmu; int g = GreatestCommonDivisor (a, B); if (g! = 1) {a/= g; B/= g;} return Fraction (a, B);} Fraction: operator-(const Fraction & fra) const {int a = fenzi, B = fenmu; a = fra. fenmu * fenzi-fra.fenzi * fenmu; B * = fra. fenmu; int g = GreatestCommonDivisor (a, B); if (g! = 1) {a/= g; B/= g;} return Fraction (a, B);} Fraction: operator * (const Fraction & fra) const {int a = fenzi, B = fenmu; a * = fra. fenzi; B * = fra. fenmu; int g = GreatestCommonDivisor (a, B); if (g! = 1) {a/= g; B/= g;} return Fraction (a, B);} Fraction: operator/(const Fraction & fra) const {return * this * Fraction (fra. fenmu, fra. fenzi);} void main () {Fraction a (1, 2); Fraction B (1, 4); Fraction c = a + B; Fraction d = a-B; fraction e = B-a; Fraction f = a * B; Fraction g = a/B; cout <"a = 1/2" <endl <"B = 1/4" <endl <"a + B =" <c <endl <"- B = "<d <endl <" B-a = "<e <endl <" a * B = "<f <endl <" a/B = "<g <endl; fraction h; cin >>h; cout 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.