Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.

Similar presentations


Presentation on theme: "Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational."— Presentation transcript:

1 Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational numbers which cannot be expressed as the ratio of two integers, such as Pi. Rational numbers are usually put into lowest terms, which is done by dividing the numerator and denominator by the greatest common denominator (GCD) of both: 4/6 -> 2/3.

2 Rationals (cont'd) Rational numbers are not a built-in type like int s and double s. Suppose that we what to work with them as a new type. We can create a new Rational class that represents rational numbers and incorporates the usual operators that we apply to numbers: add, subtract, multiply, and divide. In addition, we want to be able to read and write rational numbers in human-readable form.

3 Rational Methods The arithmetic operators all take two operands (both rational) and return a third value (also a rational). When we implement this, one of the operands will be the object that the method is called upon and the second operand will be explicitly passed as an input parameter to the method. The return value will be used to return the result. For example, to add two rational numbers together we say: c = a.plus(b);

4 Rational Operators n 1 /d 1 + n 2 /d 2 = (n 1 * d 2 + n 2 * d 1 ) / (d 1 * d 2 ) n 1 /d 1 - n 2 /d 2 = (n 1 * d 2 - n 2 * d 1 ) / (d 1 * d 2 ) n 1 /d 1 * n 2 /d 2 = (n 1 * n 2 ) / (d 1 * d 2 ) n 1 /d 1 / n 2 /d 2 = (n 1 * d 2 ) / (n 2 * d 1 )

5 Representing Rational Numbers To represent a rational number, we will need to store the numerator and the denominator, both int s. private: int n, d; We use the private modifier so that the parts cannot be accessed from outside the Rational class (information hiding).

6 A Rational Method Rational Rational::add(Rational b) { Rational c; c.n = n * b.d + b.n * d; c.d = d * b.d; return c; } The variables n and d (which are not qualified) refer to the values of the object upon which the add methods is called. In the example c = a.add(b); the add method is called on object a, so n and d refer to a 's fields.

7 Other Methods Rational Rational::subtract(Rational b) { Rational c; c.n = n * b.d - b.n * d; c.d = d * b.d; return c; } Rational Rational::multiply(Rational b) { Rational c; c.n = n * b.n; c.d = d * b.d; return c; }

8 Other Methods (cont'd) Rational Rational::divide(Rational b) { Rational c; c.n = n * b.d; c.d = b.n * d; return c; }

9 Rational Class class Rational { public: Rational add(Rational b); Rational subtract(Rational b); Rational multiply(Rational b); Rational divide(Rational b); private: int n,d; };

10 Additional Methods For the time being we will also write two additional methods so we can test our class: void Rational::set(int n0, d0) { n = n0; d = d0; } void Rational::print() { cout << n << “/” << d; }

11 Testing the Rational Class The following main program will test the methods of the Rational class: int main(void) { Rational a, b, c; a.set(1,3); b.set(2,3); c = a.plus(b); c.print(); cout << endl; } The result of this program is: 9/9

12 Oops! What have we forgotten? The rational number should be represented in lowest terms, so 9/9 isn't quite right. The GCD of the numerator and the denominator is 9, so dividing both by the GCD 9, yields 1/1, which is the rational number in lowest terms.

13 Calculating the GCD Finding the GCD is one of the oldest known algorithms, ascribed to Euclid (hence Euclid's algorithm). We find the GCD by repeatedly taking the remainder of one number divided by the other until one of the terms becomes 0. The other term is the gcd. This works because the gcd(x,y) = gcd(x, x % y).

14 Euclid's Algorithm int gcd(int a, int b) { while (b != 0) { t = b; b = a % b; a = b; }

15 New Class Definition We make this be a private method of the class, as it is not called from outside the class: class Rational { public: Rational add(Rational b); Rational subtract(Rational b); Rational multiply(Rational b); Rational divide(Rational b); private: int n,d; int gcd(int a, int b); };

16 New add method Rational Rational::add(Rational b) { Rational c; c.n = n * b.d + b.n * d; c.d = d * b.d; int g = gcd(c.n, c.d); c.n = c.n / g; c.d = c.d / g; return c; }


Download ppt "Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational."

Similar presentations


Ads by Google