Hello all,
I'm new here and I've been looking for a forum board to help with some questions. I'm fairly new to C++ programming as I am a Freshman in College who has never programmed before College so it was a big jump. Anyways, I have this homework program assignment that I am a bit confused and could use a little guidance. If any information is missing to help let me know. Thank you in advance!
This is from my textbook:
This program involves making a class for rational numbers. I have to represent rational numbers as two values of type int, one for the numerator and one for the denominator.
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 whole_number and define the constructor so that the object will be initialized to the rational number whole_number/1. also include a default constructor that initializes and object to 0. (that is, to 0/1)
Overload the input and output operators >> and <<. Numbers are to input and output in the forum 1/2, 25/32, 300/401 and so forth. Note that the numerator, denominator or both can have a minus sign.
Overload all of the following operators so that they correctly apply to the type Rational: ==. <, <=, >, >=, +, -, * and /. Also write a test program to test the class.
below is my code so far:
Code:
#include
<iostream>
using
namespace std;
class
Rational {
public:
Rational(); //Default constructor
Rational(int); //Second constructor
Rational(int, int); //Third constructor
void setData(int, int);
int getNumer(); // accessor function
int getDenom(); // accessor function
Rational operator>>(Rational &);
private:
int numerator;
int denominator;
};
Rational::Rational()
{
numerator = 0; denominator = 1;
}
Rational::Rational(int whole_number)
{
whole_number/1;
}
Rational::Rational(int num, int den)
{
numerator = num; denominator = den;
}
void Rational::setData(int num, int den)
{
numerator = num; denominator = den;
}
istream& operator>>(istream &in, Rational &rationalObj)
{
int n, d;
cout << "Enter numerator and denominator: ";
in >> n >> d;
rationalObj.setData(n, d);
return in;
}
// external non-friend version; need two accessor functions
ostream& operator<<(ostream &out, Rational &rationalObj)
{
out << rationalObj.getNumer() << "/"
<< rationalObj.getDenom();
return out;
}
intRational::getNumer()
{
return numerator;
}
intRational::getDenom()
{
return denominator;
}
int main()
{
Rational r1, r2, r3, r4;
return 0;
}