# project that requires assistance =P

Page 1 of 1

## 10 Replies - 977 Views - Last Post: 03 December 2009 - 02:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=143068&amp;s=3dd00c839c71ba5af02669a25e9cab7c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 newkid34

Reputation: 0
• Posts: 39
• Joined: 22-November 09

# project that requires assistance =P

Posted 03 December 2009 - 06:46 AM

Rational Number Class

Due Date: Mon 7 Dec 09

A rational number is the ratio of two positive integers: p/q

Arithmetic of rationals is defines as follows:

p/q + r/s = (ps + qr)/qs

p/q - r/s = (ps - qr)/qs

(p/q) * (r/s) = (pr)/(qs)

(p/q) / (r/s) = (ps)/(qr)

Implement a class named Rational that
allows us to do rational arithmetic.

It will have 2 private int vars p and q.

It will have a constructor that initializes
p and q to P and Q respectively.

so that if r1 and r2 are of type Rational,
the expressions r1 + r2, r1 - r2, r1 * r2,
and r1 / r2 evaluate to a value of type
Rational.

It will have a public member function
declared as follows:
string ToString( )
that returns "3/2" for r.ToString( ) if r is
equal to 3/2.

Extra Credit
Implement operators "==", "!=", '<', '>'

Test the class.

that's the official assignment. no user input is required, i can define whatever i want. r1 and r2 need to be rational numbers. first thing i don't know how to do is make it so that, if r1 = 3/2 then 3=P and 2=Q. if i set those = to the capitol P and Q then the private variables should also change. how can i pull those numbers from the denominator or numerator and set either = to a variable? If there is a better way, and im sure there is cause i am a newb programmer i'd love to know. any help is appreciated. Thanks.

edit1* is it possible to do this?
with P and Q defined as public variables

int r1;
r1=3/2;
P/Q = r1?

or

3/2 = P/Q;

This post has been edited by newkid34: 03 December 2009 - 07:09 AM

Is This A Good Question/Topic? 0

## Replies To: project that requires assistance =P

### #2 SwiftStriker00

• No idea why my code works

Reputation: 438
• Posts: 1,611
• Joined: 25-December 08

## Re: project that requires assistance =P

Posted 03 December 2009 - 07:08 AM

If im reading your assignment correctly r1, and r2 are created from the Rational class, which you pass in the numerator and the denomonator
```Rational r1( p, q );
Rational r2 ( num, den );

```

When you define Rational class your going to have 2 ints, P and Q, so that in your constructor:
P = p and Q = q .

Edit: Figured I should use c++ syntax

This post has been edited by SwiftStriker00: 03 December 2009 - 07:13 AM

### #3 newkid34

Reputation: 0
• Posts: 39
• Joined: 22-November 09

## Re: project that requires assistance =P

Posted 03 December 2009 - 07:11 AM

SwiftStriker00, on 3 Dec, 2009 - 06:08 AM, said:

If im reading your assignment correctly r1, and r2 are created from the Rational class, which you pass in the numerator and the denomonator
```Rational r1 = new Rational( p, q );
Rational r2 = new Rational( num, den );

```

When you define Rational class your going to have 2 ints, P and Q, so that in your constructor:
P = p and Q = q .

so correct me if im wrong but,

i can create a class just like a function and pass variables through it?
then in the MAIN function i could do something similar to what you posted?

```Rational r1 = new Rational( p, q );
Rational r2 = new Rational( num, den );

```

### #4 SwiftStriker00

• No idea why my code works

Reputation: 438
• Posts: 1,611
• Joined: 25-December 08

## Re: project that requires assistance =P

Posted 03 December 2009 - 07:20 AM

Quote

Implement a class named Rational that
allows us to do rational arithmetic.

That is the first part of your assignment, create a class that you pass the numerator and denominator.

The second part of your assignment is to overload operator+ so your pogram knows how to do this:

```Rational r1( 3, 2 );
Rational r2( 6, 4 );
Rational result = r1 + r2;
cout << result.toString() << endl;
//cout should be "12/4" or "3" if your prof wants you to reduce

```

### #5 newkid34

Reputation: 0
• Posts: 39
• Joined: 22-November 09

## Re: project that requires assistance =P

Posted 03 December 2009 - 10:44 AM

```#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

class Rational
{
public:
Rational();
// Initializes the rational number to 0

Rational (int wholeNumber);
// Initializes the rational number to wholeNumber/1

Rational (int m, int n);
// Initializes the rational number to m/n if n is not 0;

void output();
// The rational number has been displayed on
// the screen in the form m/n.

friend Rational operator +(const Rational& r1, const Rational& r2);
// Returns the sum of r1 and r2

friend Rational operator *(const Rational& r1, const Rational& r2);
// Returns the product of r1 and r2

friend Rational operator -(const Rational& r1, const Rational& r2);
// Returns the Difference of r1 and r2

friend Rational operator /(const Rational& r1, const Rational& r2);
// Returns the Dividend of r1 and r2
private:
int p;	 // the numerator of the number
int q;   // the denominator of the number

void standardize();

};

// =======================
//	 main function
// =======================
int main ()
{
//
// Variable declarations
//
//char doAgain;	 for the loop control
int m, n;		// numerator and denominator is m/n
Rational sum, product, difference, dividend;

// cout << endl << "Testing sum and product..." << endl << endl; //

//
// Read and construct two rational numbers
//

/*
cout << "Enter two rational numbers (numerator and denominator "
<< "separated by a blank)." << endl << endl;
cout << "Numerator and denominator of the first rational number: ";
cin >> m >> n;
cout << endl;
*/

m = 3;
n = 2;

Rational r1(m, n);
/*
cout << "Numerator and denominator of the second rational number: ";
cin >> m >> n;
cout << endl;
*/

m=3;
n=2;
Rational r2(m, n);

//
// Compute the sum, product, difference and dividend
//
sum = r1 + r2;
product = r1 * r2;
difference = r1 - r2;
dividend = r1 / r2;

//
// Print out the results
//
cout << "The sum of ";
r1.output();
cout << " and ";
r2.output();
cout << " is ";
sum.output();
cout << endl;
cout << "The product is ";
product.output();
cout << endl << endl;

return 0;
}

// ===========================
//	Function Definitions
// ===========================
Rational::Rational()
{
p = 0;
q = 1;
}

Rational::Rational (int wholeNumber)
{
p = wholeNumber;
q = 1;
}

Rational::Rational (int m, int n)
{

p = m;
q = n;
standardize();

}

void Rational::standardize()
{
if (q < 0)
{
q = -1 * q;
p = -1 * p;
}
else if (q == 0)
{
cout << "Zero cannot be in the denominator - program aborting!!"
<< endl;
exit(1);
}
}

void Rational::output()
{
cout << p << "/" << q;
}

Rational operator + (const Rational& r1, const Rational& r2)
{
int p = r1.p () * r2.q() + r2.p () * r1.q ();
int q = r1.q () * r2.q ();
Rational sum (p, q);
return sum;
}

```

this is what ive got and i think it looks good, i just need some help setting up the functions for overloading the operators + , - , * , /.
=P.

EDIT*
when i put this in there i get

term does not evaluate to a function containing 0 arguments.

This post has been edited by newkid34: 03 December 2009 - 11:11 AM

### #6 SwiftStriker00

• No idea why my code works

Reputation: 438
• Posts: 1,611
• Joined: 25-December 08

## Re: project that requires assistance =P

Posted 03 December 2009 - 11:02 AM

### #7 newkid34

Reputation: 0
• Posts: 39
• Joined: 22-November 09

## Re: project that requires assistance =P

Posted 03 December 2009 - 11:38 AM

SwiftStriker00, on 3 Dec, 2009 - 10:02 AM, said:

read what you posted and im still confused but i did get a little further.

```#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

class Rational
{
public:
Rational();
// Initializes the rational number to 0

int numerator () const;
int denominator () const;

Rational (int wholeNumber);
// Initializes the rational number to wholeNumber/1

Rational (int m, int n);
// Initializes the rational number to m/n if n is not 0;

void output();
// The rational number has been displayed on
// the screen in the form m/n.

friend Rational operator +(const Rational& left, const Rational& right);
// Returns the sum of r1 and r2

friend Rational operator *(const Rational& left, const Rational& right);
// Returns the product of r1 and r2

friend Rational operator -(const Rational& left, const Rational& right);
// Returns the Difference of r1 and r2

friend Rational operator /(const Rational& left, const Rational& right);
// Returns the Dividend of r1 and r2
private:
int p;	 // the numerator of the number
int q;   // the denominator of the number

void standardize();

};

// =======================
//	 main function
// =======================
int main ()
{
//
// Variable declarations
//
//char doAgain;	 for the loop control
int m, n;		// numerator and denominator is m/n
Rational sum, product, difference, dividend;

// cout << endl << "Testing sum and product..." << endl << endl; //

//
// Read and construct two rational numbers
//

/*
cout << "Enter two rational numbers (numerator and denominator "
<< "separated by a blank)." << endl << endl;
cout << "Numerator and denominator of the first rational number: ";
cin >> m >> n;
cout << endl;
*/

m = 3;
n = 2;

Rational r1(m, n);
/*
cout << "Numerator and denominator of the second rational number: ";
cin >> m >> n;
cout << endl;
*/

m=3;
n=2;
Rational r2(m, n);

//
// Compute the sum, product, difference and dividend
//
sum = r1 + r2;
product = r1 * r2;
difference = r1 - r2;
dividend = r1 / r2;

//
// Print out the results
//
cout << "The sum of ";
r1.output();
cout << " and ";
r2.output();
cout << " is ";
sum.output();
/*
cout << endl;
cout << "The product is ";
product.output();
cout << endl << endl;
*/

return 0;
}

// ===========================
//	Function Definitions
// ===========================
Rational::Rational()
{
p = 0;
q = 1;
}

Rational::Rational (int wholeNumber)
{
p = wholeNumber;
q = 1;
}

Rational::Rational (int m, int n)
{

p = m;
q = n;
standardize();

}

void Rational::standardize()
{
if (q < 0)
{
q = -1 * q;
p = -1 * p;
}
else if (q == 0)
{
cout << "Zero cannot be in the denominator - program aborting!!"
<< endl;
exit(1);
}
}

void Rational::output()
{
cout << p << "/" << q;
}

int Rational::denominator () const
{
return q;
}
int Rational::numerator() const
{
return p;
}

Rational operator +(const Rational& left, const Rational& right)const
{
int numerator = left.numerator () * right.denominator () + right.numerator () * left.denominator ();
int denominator = left.denominator () * right.denominator ();
Rational sum (numerator, denominator);
return sum;
}

```

now i only get an error saying

error C2270: '+' : modifiers not allowed on nonmember functions

when i double click the error it goes to this function

Rational operator +(const Rational& left, const Rational& right)const
{
int numerator = left.numerator () * right.denominator () + right.numerator () * left.denominator ();
int denominator = left.denominator () * right.denominator ();
Rational sum (numerator, denominator);
return sum;
}

This post has been edited by newkid34: 03 December 2009 - 11:39 AM

### #8 newkid34

Reputation: 0
• Posts: 39
• Joined: 22-November 09

## Re: project that requires assistance =P

Posted 03 December 2009 - 12:11 PM

```#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

class Rational
{
public:
Rational();
// Initializes the rational number to 0

int numerator ();
int denominator ();

Rational (int wholeNumber);
// Initializes the rational number to wholeNumber/1

Rational (int m, int n);
// Initializes the rational number to m/n

void output();
// The rational number is displayed

friend Rational operator +( Rational& left,  Rational& right);
// Returns the sum of r1 and r2

friend Rational operator *( Rational& left,  Rational& right);
// Returns the product of r1 and r2

friend Rational operator -( Rational& left,  Rational& right);
// Returns the Difference of r1 and r2

friend Rational operator /( Rational& left,  Rational& right);
// Returns the Dividend of r1 and r2
private:
int p;	 // the numerator of the number
int q;   // the denominator of the number

void standardize();

};

// =======================
//	 main function
// =======================
int main ()
{
int m, n;		// numerator and denominator is m/n
Rational sum, product, difference, dividend;

m = 3;
n = 2;

Rational r1(m,n);

m=3;
n=2;
Rational r2(m, n);

//
// Compute the sum, product, difference and dividend
//
sum = r1 + r2;
product = r1 * r2;
difference = r1 - r2;
dividend = r1 / r2;

//
// Print out the results
//
cout << "The sum of ";
r1.output();
cout << " and ";
r2.output();
cout << " is ";
sum.output();
cout << endl;
cout << "The Difference is ";
difference.output();
cout << endl << endl;
cout << "The product is ";
product.output();
cout << endl;
cout << "The dividend is ";
dividend.output();
cout << endl;

getchar();
getchar();
return 0;
}

// ===========================
//	Function Definitions
// ===========================
Rational::Rational()
{
p = 0;
q = 1;
}

Rational::Rational (int wholeNumber)
{
p = wholeNumber;
q = 1;
}

Rational::Rational (int m, int n)
{

p = m;
q = n;

}

void Rational::standardize()
{
if (q < 0)
{
q = -1 * q;
p = -1 * p;
}
else if (q == 0)
{
cout << "Zero cannot be in the denominator - program aborting!!"
<< endl;
exit(1);
}
}

void Rational::output()
{
cout << p << "/" << q;
}

int Rational::denominator ()
{
return q;
}
int Rational::numerator()
{
return p;
}

Rational operator +( Rational& left, Rational& right)
{
int numerator = left.numerator () * right.denominator () + right.numerator () * left.denominator ();
int denominator = left.denominator () * right.denominator ();
Rational sum (numerator, denominator);
return sum;
}

Rational operator -( Rational& left,  Rational& right)
{
int numerator = left.numerator () * right.denominator () - right.numerator () * left.denominator ();
int denominator = left.denominator () * right.denominator ();
Rational difference (numerator, denominator);
return difference;
}

Rational operator *( Rational& left,  Rational& right)
{
int numerator = left.numerator () * right.numerator ();
int denominator = left.denominator () * right.denominator ();
Rational product (numerator, denominator);
return product;
}

Rational operator /( Rational & left,  Rational & right)
{
int numerator = left.numerator () * right.denominator ();
int denominator = left.denominator () * right.numerator ();
Rational dividend (numerator, denominator);
return dividend;
}

```

THIS CODE WORKS!!!! YAY!!!

lol, but one last thing i dont NEED to do but it is in the directions. could someone help me create the public member function string ToString();
so that instead of r1.output/r2.output sum.output etc. i can use r1.ToString() etc....

Thanks everyone.

### #9 SwiftStriker00

• No idea why my code works

Reputation: 438
• Posts: 1,611
• Joined: 25-December 08

## Re: project that requires assistance =P

Posted 03 December 2009 - 12:30 PM

you need to override toString

This post has been edited by SwiftStriker00: 03 December 2009 - 12:30 PM

### #10 newkid34

Reputation: 0
• Posts: 39
• Joined: 22-November 09

## Re: project that requires assistance =P

Posted 03 December 2009 - 02:34 PM

SwiftStriker00, on 3 Dec, 2009 - 11:30 AM, said:

you need to override toString

string ToString()
{
return &Rational::numerator + " " + &Rational::denominator;
}

if i use this within the Rational class, i get this error

error C2296: '+' : illegal, left operand has type 'int (__thiscall Rational::* )(void)'

any ideas on how to fix that one?

i tried to do it the way it was in that guide you sent me but it didn't work =/, that was C#. idk if that has anything to do with it or if i was just wrong.

### #11 newkid34

Reputation: 0
• Posts: 39
• Joined: 22-November 09

## Re: project that requires assistance =P

Posted 03 December 2009 - 02:48 PM

haha figured out my own workaround, but will it fly?

i changed

void output();

to

void ToString();

and everywhere there was the word output i changed to ToString. now it reads

r1.ToString();
r2.ToString();

and so forth.

do you all think this is ok?

technically the instructions say

member function

string ToString();

This post has been edited by newkid34: 03 December 2009 - 02:50 PM