Here is my assignment:

"Define a class for rational numbers. A rational number is a number that can be expressed as the quotient of two integers. For example, ½, ¾, 64/2, and so forth are all rational numbers. (By ½ and so on we mean the every day fraction, not the integer division this expression would produce in a C++ program.) Represent rational numbers as two values of type int, one for the numerator and one for the denominator. Call the class Rational. Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also, include a constructor that has only a single parameter of type int; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1. Include a default constructor that initializes an object to 0 (that is, to 0/1). Overload the input and output operators >> and <<. Numbers are to be input and output in the form ½, 15/32, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so -1/2, 15/-32, and -300/-401 are also possible inputs. Overload all the following operators so that they correctly apply to the type Rational: ==, <, <=, >,>=, +, -, *, and /. Write a test program to test your class. Hints: two rational numbers a/b and c/d are equal if a*d equals c*b. If b and d are positive rational numbers, a/b is less than c/d provided a*d is less than c*b. You should include a function to normalize the values stored so that after normalization the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization, 4/-8 would be expressed as -1/2. "

I was wonder if I am off on the right track with the following class and declarations....?

//Paul Baker
//TRCC Intermediate C++
//Assignment 4, Rational Numbers

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

using namespace std;

#include <iostream>

class Rational
{
  public:
    Rational( int = 0, int = 1 );
    Rational( const Rational& );
    Rational& operator=( const Rational& );
    int GCD();
    void Simplify();
    bool IsValid() const;
    int Numerator() const;
    int Denominator() const;
    friend istream& operator>>( istream&, Rational& );
    friend ostream& operator<<( ostream&, const Rational& );

  private:
    int Numerator_;
    int Denominator_;
};

bool operator==( const Rational&, const Rational& );
bool operator!=( const Rational&, const Rational& );
bool operator< ( const Rational&, const Rational& );
bool operator<=( const Rational&, const Rational& );
bool operator> ( const Rational&, const Rational& );
bool operator>=( const Rational&, const Rational& );

Rational operator+( const Rational&, const Rational& );
Rational operator-( const Rational&, const Rational& );
Rational operator*( const Rational&, const Rational& );
Rational operator/( const Rational&, const Rational& );

Recommended Answers

All 4 Replies

Is there a reason on why the overloaded operators are not
part of the Rational class? I think the GCD and simplify function need
not to be there. Why shouldn't the class automatically simplify the numerator and the denominator?
I can't think of any of the top of my head.
If there is no reason that the rational should not simplify the fraction in
the constructor, then there is no need for gcd. And thus no need for
simplify() function either. Other than thats, your on the right track.
Also what isValid() doing? And make sure the denominator is not 0 at all times.

As far as Rational( int = 0, int = 1 ); , test that way of writing it to make sure it works. A more effective way to write it would be Rational(int num=0,int den=1):Numerator_(num),Denominator(den) {}

I can't seem to go any further this this point, due to errors, and I can't figure out what they mean:

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Rational::Rational(void)" (??0Rational@@QAE@XZ) referenced in function _main RationalNumbers.obj Assignment4

Error 2 error LNK1120: 1 unresolved externals H:\Documents\Baker School\TRCC C++\Assignment4\Debug\Assignment4.exe Assignment4

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

class Rational
{
  public:
	Rational();
	Rational( int MytopNumber, int MybottomNumber);
	Rational( int wholeNumber);
	void input();
	void output();

private:
    int Numerator;
    int Denominator;
};

bool operator==( const Rational&, const Rational& );
bool operator!=( const Rational&, const Rational& );
bool operator< ( const Rational&, const Rational& );
bool operator<=( const Rational&, const Rational& );
bool operator> ( const Rational&, const Rational& );
bool operator>=( const Rational&, const Rational& );

const Rational operator+( const Rational&, const Rational& );
const Rational operator-( const Rational&, const Rational& );
const Rational operator*( const Rational&, const Rational& );
const Rational operator/( const Rational&, const Rational& );

void main()
{
	Rational MytopNumber, MybottomNumber;

	cout << "Please Enter Numerator: " << endl;
	MytopNumber.input( );

	cout << "Rational Number Calculator Processing" << endl;

}

void Rational::input( )
{
	int MytopNumber;

	cin >> MytopNumber;
}

I take it that this is your first time using classes

the first error tells you that there is no definition to the constructor

which it needs to create the object, as this can be in a .cpp file the compiler won't complain but the linker will!

so somewhere you need a piece of code like jonsca shows
just like your input function

//constructors have no return type
Rational::Rational()
{
//mthods go in here
}

Rational::Rational(int num, int den)
{
}

so link errors means that you either haven't written a function you use ot included the file in your project.

Error 1 error LNK2019: unresolved external symbol "public: __thiscall Rational::Rational(void)" (??0Rational@@QAE@XZ) referenced in function _main RationalNumbers.obj Assignment4

Be a part of the DaniWeb community

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