Transcript Document

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.
Rationals (cont'd)
Rational numbers are not a built-in type like ints
and doubles. 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.
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);
Rational Operators
n1/d1 + n2/d2 = (n1 * d2 + n2 * d1) / (d1 * d2)
n /d - n /d = (n * d - n * d ) / (d * d )
1 1
2 2
1
2
2
1
1
2
n /d * n /d = (n * n ) / (d * d )
1 1
2 2
1
2
1
2
n /d / n /d = (n * d ) / (n * d )
1 1
2 2
1
2
2
1

Representing Rational Numbers
To represent a rational number, we will need to store
the numerator and the denominator, both ints.
private:
int n, d;
We use the private modifier so that the parts cannot
be accessed from outside the Rational class
(information hiding).
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.
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;
}
Other Methods (cont'd)
Rational Rational::divide(Rational b) {
Rational c;
c.n = n * b.d;
c.d = b.n * d;
return c;
}
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;
};
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;
}
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
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.
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).
Euclid's Algorithm
int gcd(int a, int b) {
while (b != 0) {
t = b;
b = a % b;
a = b;
}
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);
};
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;
}