``````Hi

Im completely new to programming and was wondering if anyone would be willing to help me with this exercise in C++. I have absolutely no idea how to do this exercise:
-----------------------------------------------

Rational.h is a C++ header file which declares a class (Rational) which handles Rational numbers (i.e. fractions).

You must create a C++ file which contains the definitions for all the methods in that class. Rational.cc is the start of just such a file, which contains one method to get you started.
Once you have the class methods defined, you can use it with the TestRat.cc test driver program. Using g++ you would perform the following steps:

1. g++ -c Rational.cc //this creates Rational.o with the class definitions in it
2. g++ -c TestRat.cc //this creates TestRat.o with the main function in it
3. g++ Rational.o TestRat.o -o TestRat //this combines the class definitions with the class usage in the main function and creates an executable file called TestRat

Rational.h is given here:

const int MAXB=20;

class Rational {

private:
int numerator, denominator;
char buffer[MAXB];

static int gcd (int a, int b); //a helpful function that returns the
//Greatest Common
Denominator of
//two numbers

void normalize(); //Make sure that this fraction is in
//its lowest terms (ie 3/12 -> 1/4)
//Hint: use gcd()

public:
Rational(); //default constructor of a Rational
//number whose numerator and
//denominator are 0/1
Rational(int n, int d); //constructor of a Rational number
//whose numerator and denominator
//are n/d

char *toString(); //turns this number into a string
//using this object's handy buffer

Rational plus(Rational r); // add two rational numbers
Rational minus(Rational r); // subtract two rational numbers
Rational times(Rational r); // multiply two rational numbers
Rational divide(Rational r); // divide two rational numbers

// don't forget that
// a/b + c/d == (ad + bc) / bd
// a/b - c/d == (ad - bc) / bd
// a/b * c/d == ac / bd
// a/b / c/d == ad / bc
};

Rational.cc is given here:

#include <stdio.h>
#include "Rational.h"

int Rational::gcd (int a, int b){
if (b == 0) return a;
else return gcd (b, a % b);
}

TestRat.cc is given here:

#include <iostream.h>
#include "Rational.h"

int main(int argc, char *argv[]){
Rational a(2,3);
Rational b(1,2);

Rational res;

res=a.plus(b);
cout << a.toString() << " + " << b.toString() << " = " <<
res.toString() << endl ;

res=a.minus(b);
cout << a.toString() << " - " << b.toString() << " = " <<
res.toString() << endl ;

res=a.times(b);
cout << a.toString() << " * " << b.toString() << " = " <<
res.toString() << endl ;

res=a.divide(b);
cout << a.toString() << " / " << b.toString() << " = " <<
res.toString() << endl ;

}``````

--------------------------------------------------------------------------------
If someone could possibly do the exercise and run my through it, i would be really grateful.

Thanks alot

>>If someone could possibly do the exercise and run my through it, i would be really grateful.

I'm sure you would be gradeful. I'll be grateful too if you deposite \$10,000.00 USD in my PayPal account. After you do that then I'll gladly email you the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.