/* class Rational to practice basics of defining and using classes. */ #include #include using namespace std; /* * class Rational * represents a Rational number. Remember rational means ratio-nal * which means there is a numerator and denominator implemented using * integer values. Using good ADT techniques, we have made member * variable private (also known as instance variables) and made member * functions public. */ class Rational { public: Rational (); // default constructor Rational(int i); // constructor that takes a single int as paramter. Implies that the // denominator is 1. Rational (int p, int q); //constructor that takes two ints as parameter. Verify q is not zero void input(); // read value for the object from standard input void output(); // display the value of the object to standard output //Todo: declare an accessor (getter function) that returns the numerator //Todo: declare an accessor (getter function) that returns the denominator void Sum (Rational op1, Rational op2); // set current object's value to be the sum of num1 and num2 bool isEqual(const Rational& op); // test if two rational numbers are equal. private: int numerator; int denominator; }; /* * main program * Unit Test code for class Rational uses all defined methods */ int main () { Rational a(1,2); // a is 1/2 a.output (); Rational b(2); // b is 2/1 b.output (); Rational c; // what does the default constructor do? c.output (); c.input (); // read value for c from input c.output (); c.Sum (a,b); // c will be set to 1/2+2/1 = 5/2 c.output (); //this should display 5/2 //Todo: display the numerator of c //Todo: display the denominator of c //Todo: read in two rational numbers and test if they are equal using isEqual Rational d; Rational e; // Todo: get the values using the input member function // Todo: compare using isEquals and write out the result. } //Definitions of all member functions