I am working on a program that uses a custom class for performing arithmetic equations on rational numbers. I will end with functions that multiply, divide, add, and subtract rationals, and store the answers in simplest form.
Everything is going okay so far except for one thing. I need and have a custom default constructor to set the values for any Rational.object that may not be given a value when instantiated.
Example is
Rational answer();
This creates the answer object of type Rational. With my custom constructor it is supposed to store a numerator at 0 and a denominator at 1. However when I attempt to create an object without passing any value and then attempt to use that object on the left side of an equation it yells at me.
Here is my code for the header file:
and here is my code for the client and member functions:
Now as you can see I create a Rational object in the multiply_Rationals function called 'results' and another Rational object in the main called 'answer'.
The program runs fine now, but I would like to omit the values that I am passing into those two objects and have the custom constructor that I made set the objects to their default values. When I take out the values that I am passing in though it bombs. Any thoughts???
Everything is going okay so far except for one thing. I need and have a custom default constructor to set the values for any Rational.object that may not be given a value when instantiated.
Example is
Rational answer();
This creates the answer object of type Rational. With my custom constructor it is supposed to store a numerator at 0 and a denominator at 1. However when I attempt to create an object without passing any value and then attempt to use that object on the left side of an equation it yells at me.
Here is my code for the header file:
Code:
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational();
Rational(int, int);
void print_Rational();
Rational multiply_Rationals(Rational);
private:
int numerator;
int denominator;
};
#endif
and here is my code for the client and member functions:
Code:
#include <iostream>
#include <iomanip>
#include "Rational.h"
using namespace std;
Rational::Rational()
{
numerator = 0;
denominator = 1;
};
Rational::Rational(int n, int d)
{
int max;
Rational result;
(n>d) ? max = n:max = d;
for(int i=2;i<=max/2;i++)
{
if(n%i ==0 && d%i ==0)
{
numerator = n/i;
denominator = d/=i;
}
}
};
Rational Rational::multiply_Rationals(Rational second_rational)
{
int num_Result;
int den_Result;
Rational results(0,1);
num_Result = ( numerator * second_rational.numerator );
den_Result = ( denominator * second_rational.denominator );
Rational(num_Result, den_Result);
results.numerator = num_Result;
results.denominator = den_Result;
return Rational(results.numerator, results.denominator);
}
void Rational::print_Rational()
{
cout << numerator << "/" << denominator << endl;
}
int main()
{
Rational f1(7,14);
Rational f2(4,6);
Rational answer(0,1);
answer = f1.multiply_Rationals(f2);
f1.print_Rational();
cout << "Times " << endl;
f2.print_Rational();
cout << "equals" << endl;
answer.print_Rational();
system("PAUSE");
}
Now as you can see I create a Rational object in the multiply_Rationals function called 'results' and another Rational object in the main called 'answer'.
The program runs fine now, but I would like to omit the values that I am passing into those two objects and have the custom constructor that I made set the objects to their default values. When I take out the values that I am passing in though it bombs. Any thoughts???
Last edited:
