I defined a Fraction class as following, overloading the "=" & "+" operators.
I made this program as simple as possible to show the problem.
#include <iostream>
class Fraction {
private:
int nominator;
int denominator;
public:
Fraction ();
Fraction (int, int);
Fraction & operator = (Fraction &);
friend Fraction operator + (Fraction &, Fraction &);
// static function:
// find the Greatest Common Divisor of two numbers
int static GCD(int x, int y);
};
int Fraction::GCD(int x, int y) {
if (y == 0) {
return x;
} else {
return GCD (y, x % y);
}
}
Fraction::Fraction () {
nominator = NULL;
denominator = NULL;
}
Fraction::Fraction (int num_1, int num_2) {
int divisor = Fraction::GCD (num_1, num_2);
nominator = num_1 / divisor;
denominator = num_2 / divisor;
}
Fraction & Fraction::operator = (Fraction &A) {
nominator = A.nominator;
denominator = A.denominator;
return *this;
}
Fraction operator + (Fraction &A, Fraction &B) {
int nominator = A.nominator * B.denominator + B.nominator * A.denominator;
int denominator = A.denominator * B.denominator;
int divisor = Fraction::GCD (nominator, denominator);
return Fraction (nominator / divisor, denominator / divisor);
}
And in the Main() function, I have three test case
int main(int argc, const char * argv[]) {
Fraction frac_a = Fraction(1, 3);
Fraction frac_b = Fraction(1, 4);
// test 1: no compile error
frac_a + frac_b;
frac_a = frac_b;
// test 2: no compile error
Fraction frac_c = frac_a + frac_b;
// test 3: Error: No viable overloaded '='
Fraction frac_d;
frac_d = frac_a + frac_b;
return 0;
}
The question is, why "test 3" has "No viable overloaded '='" error?
Your assignment operator is declared to take a reference to a non-const Fraction. Your addition operator returns by-value, so you are trying to assign a temporary to frac_c. Temporaries can not bind to non-const references.
The fix is to make your assignment operator take by const-reference:
Fraction & operator = (const Fraction &);
That's because your operator= can't bind to temporaries.
You should define it like this:
Fraction & Fraction::operator = (const Fraction &A)
so, it will bind to temporaries and constant objects and literals.
You defined the assignment operator= to take an lvalue reference as argument, but tried to pass it a temporary. Temporaries only bind to const references or rvalue references.
You should read up on how to overload operators in a usable way, e.g. the same problem occurs with the nonconst lvalue references accepted by your operator+.
That means, use Fraction& operator=(Fraction const&) and Fraction operator+(Fraction const&, Fraction const&).
Related
class Fraction
{
friend Fraction operator+(const Fraction& f1, const Fraction& f2);
private:
int numerator;
int denominator;
};
--------------------------------------------------------------------
Fraction operator+(const Fraction& f1, const Fraction& f2)
{
Fraction r; // declare a Fraction to hold the result
// load result Fraction with sum of adjusted numerators
r.numerator = (f1.numerator*f2.denominator)
+ (f2.numerator*f1.denominator);
// load result with the common denominator
r.denominator = f1.denominator * f2.denominator;
return r; // return the result Fraction
}
Is there a way to make Operator+ add fraction (from Fraction class) with integer?
Example:
Fraction frac(7/15), x = frac + 13;
Yes it is possible, although I wouldn't do it that way.
Presumably (in your real code) you have a constructor for Fraction that takes two arguments: a numerator and a denominator. Make the denominator a default argument set to 1.
Then you will not need any more overloads to +, or any extra overloads that you intend to support over time (e.g. -, *, &c.).
I have tried to add more features to already given class(the answer of #Aak) in the problem: How to output fraction instead of decimal number?
for printing fractions in numerator / denominator .
First of all, the given code was not working without any change in the code. Then I made it working after making some changes. However, my implementation gives me a wrong output.
for example:
input: A = 3;
B = 3;
Output: 9/1
9
instead of: 1
here is the complete implementation:
#include <iostream>
/********************** Rational class **********************************/
class Rational
{
private:
int m_numerator, m_denominator;
private:
inline void simplificate()
{
int commondivisor = 1;
for(int i=2;i<= std::min(abs(m_numerator), abs(m_denominator));i++)
if( m_numerator%i == 0 && m_denominator%i == 0 )
commondivisor = i;
m_numerator /= commondivisor;
m_denominator /= commondivisor;
}
public:
Rational() // Defualt
:m_numerator(1), m_denominator(1)
{}
Rational(const int& num, const int& den=1) // Parameterized
:m_numerator(num), m_denominator(den)
{}
Rational(const Rational& other) // Copy
:m_numerator(other.m_numerator), m_denominator(other.m_denominator)
{}
/*Rational(Rational&& other) // Move
:m_numerator(other.m_numerator), m_denominator(other.m_denominator)
{}*/
~Rational(){}
Rational& operator/ (const int& divisor)
{
m_denominator *= divisor;
simplificate();
return *this;
}
Rational& operator/ (const Rational &divisor)
{
m_numerator *= divisor.m_numerator;
m_denominator *= divisor.m_denominator;
simplificate();
return *this;
}
const double getrealformat()const
{
return static_cast<double>(m_numerator)/
static_cast<double>(m_denominator);
}
friend double operator/ (Rational& obj, const int& divisor);
friend void printRational(Rational& obj, const int& A, const int& B);
friend void printRational(Rational& obj, const int&& A, const int&& B);
};
/************************** Friend functions ********************************/
double operator/ (Rational& obj, const int& divisor)
{
obj.m_denominator *= divisor;
obj.simplificate();
return obj.getrealformat();
}
void printRational(Rational& obj, const int& A, const int& B) // lvalue
{
Rational r1(A), r2(B);
obj = r1/r2;
std::cout<<obj.m_numerator<<'/'<<obj.m_denominator<<std::endl;
std::cout<<obj.getrealformat()<<std::endl;
}
void printRational(Rational& obj, const int&& A, const int&& B) // rvalue
{
Rational r1(A), r2(B);
obj = r1/r2;
std::cout<<obj.m_numerator<<'/'<<obj.m_denominator<<std::endl;
std::cout<<obj.getrealformat()<<std::endl;
}
/*****************************************************************************/
int main()
{
Rational obj;
printRational(obj, 3,3);
return 0;
}
Question - 1: the logic looks fine, but I don't know why I am getting the wrong answer. Can anybody find the problem?
Question - 2: I have written "Move" constructor for the class, which you can find in the commented section. However, I could not use it because of following error:
D:\Programming\C++\CPP Programs\Class - Fractions\Class - Fractions.cpp|70|error: use of deleted function 'Rational& Rational::operator=(const Rational&)'|
D:\Programming\C++\CPP Programs\Class - Fractions\Class - Fractions.cpp|77|error: use of deleted function 'Rational& Rational::operator=(const Rational&)'|
(whenever it has been called the moved object/instance is destroyed, to my knowledge.)
can anybody help me to implement the Move constructor for this class?
Look your operator/()
Rational& operator/ (const Rational &divisor)
{
m_numerator *= divisor.m_numerator;
m_denominator *= divisor.m_denominator;
simplificate();
return *this;
}
This code is correct for operator*(), not for operator/().
Maybe
m_numerator *= divisor.m_denominator;
m_denominator *= divisor.m_numerator;
But is worse that you're operator/() modify the object.
Your code (corrected switching numerator and denominator) should be correct for operator/=(), not for operator/() that should return a new object.
I suggest something as follows
Rational& operator/= (const Rational &divisor)
{
m_numerator *= divisor.m_denominator;
m_denominator *= divisor.m_numerator;
simplificate();
return *this;
}
friend Rational operator/ (Rational A, Rational const & B);
and, outside the class,
Rational operator/ (Rational A, Rational const & B)
{ return A/=B; }
Regarding question 2 ("I have written "Move" constructor for the class, [...]However, I could not use it because of following error"), you can see in this page that
A implicitly-declared copy assignment operator for class T is defined as deleted if any of the following is true:
T has a user-declared move constructor;
T has a user-declared move assignment operator.
So, when you define the move contructor, you delete the implicit copy operator.
You can solve the problem adding
Rational & operator= (Rational const &) = default;
reactivating the implicit copy constructor
I am getting no match for 'operator +' error in main section.
I overloaded all of the operators as friend except =
I tried to debug but could not figured it out
I don't know, what I am skipping.
Please advise.
#include <iostream>
using namespace std;
class fraction
{
long num;
long den;
public:
fraction( ){num = 0L ; den = 1L ;}
fraction(long l_num,long l_den){num = l_num ; den = l_den ;}
fraction(const fraction& f){num=f.num; den=f.den;}
~fraction ( ){}
long getNum (void) const{return num ;}
long getDen (void) const{return den ;}
void print(void)
{
if(den==0)den=1L;
cout<<num<<"/"<<den <<endl;
}
fraction inc ( )
{
long numX = (num) + (den);
long denY = (den);
return fraction(numX, denY );
}
friend fraction operator +(fraction &f1 , fraction &f2);
friend fraction operator +(int i, fraction &f2);
friend fraction &operator++(fraction & f1);
fraction operator =( const fraction &f);
friend fraction operator -(fraction &f1 , fraction &f2);
friend fraction operator *(fraction &f1 , fraction &f2);
friend fraction operator /(fraction &f1 , fraction &f2);
};
inline fraction operator +(fraction &f1, fraction &f2)
{
fraction temp;
temp.num=(f1.num*f2.den)+(f1.den*f2.num);
temp.den=f1.den*f2.den;
return temp;
}
inline fraction operator +(int i, fraction &f2)
{
fraction temp;
temp.num=i*f2.den+f2.num;
temp.den=f2.den;
return temp;
}
inline fraction &operator++(fraction& f1)
{
f1.num=f1.num+f1.den;
return f1;
}
inline fraction fraction::operator=(const fraction &f)
{
num=f.num;
den=f.den;
return*this;
}
inline fraction operator -(fraction &f1, fraction &f2)
{
fraction temp;
temp.num=(f1.num*f2.den)-(f1.den*f2.num);
temp.den=f1.den*f2.den;
return temp;
}
inline fraction operator *(fraction &f1, fraction &f2)
{
fraction temp;
temp.num=f1.num*f2.num;
temp.den=f1.den*f2.den;
return temp;
}
inline fraction operator /(fraction &f1 , fraction &f2)
{
fraction temp;
temp.num=f1.num*f2.den;
temp.den=f1.den*f2.num;
return temp;
}
int main ( )
{
fraction f1, f2(2L,0L);
fraction f3(f2);
f1.print();
f2.print();
f3.print();
f3 = f3 + fraction (-5 , 4);
f1 = f2 + f3;
f1.print ( );
f1 = f2 - f3;
f1.print();
f1 = f2 * f3;
f1.print();
(++ (++f1)).print();
f1 = f2 / f3;
f1.print();
f1 = 5 + f1;
f1.print();
return 0;
}
All of your operators take their parameters as references to non-const fraction. Which means they can only be called with non-const lvalues of type fraction. A temporary (such as the result of fraction(-5, 4)) is an rvalue, not an lvalue, and thus cannot be used as an argument to your operators.
I dare say it's wrong for arithmetic operators to take non-const references as parameters: that indicates they will modify their operands, which is plain wrong. You should change all the binary operators' parameters from fraction & to const fraction &.
A reference to const allows rvalues to bind it to, so your code will work as expected.
Of course, this doesn't apply to operator++ (that's why I mention binary operators above). ++ actually modifies its operand, so in its case, a reference to non-const is the correct approach. The same would apply to compound assignment operators such as +=.
This line is causing all the errors
f3 = f3 + fraction (-5 , 4)
You are using fraction operator +(fraction &f1 , fraction &f2); function to add an object and an anonymous object.
But anonymous object are only temporary. Read this answer.
Remove that line and your code should work.
Edit:
Might work if you change
fraction operator +(fraction &f1 , fraction &f2);
to
fraction operator +(fraction &f1 , fraction f2);
I have two classes Integer and Fraction and one abstract class Number. I am suppose to perform addtion operation by overloading + and also I need to check equality of values using overloading of == operator on these classes.
Operations to be performed
1. Add Integer + Integer = Integer
2. Add Fraction + Fraction = Fraction
3. Add Integer + Fraction = Fraction
I have been able to do 1st and 2nd operation but not able to do addition of integer and fraction.
Below is the code snippet:
Number.h
#pragma once
#include <iostream>
template<class T>
class Number
{
virtual const T operator+ (const T &) = 0;
virtual void display(std::ostream &) const = 0;
virtual bool operator==(const T& rhs) const = 0;
};
Integer.h
#pragma once
#include "Number.h"
#include "Fraction.h"
class Integer : public Number<Integer>
{
int intValue;
public:
void display(std::ostream &) const;
int getValue() const;
void setValue(int);
Integer() {}
Integer(int num);
const Integer operator+ (const Integer &);
virtual ~Integer() {}
bool operator==(const Integer&) const;
};
Integer.cpp
#include "Integer.h"
#include "Number.h"
#include <iostream>
#include <string>
// parameterized constructor
Integer::Integer(int num)
{
intValue = num;
}
// return integer value
int Integer::getValue() const
{
return this->intValue;
}
void Integer::setValue(int x)
{
this->intValue = x;
}
// operator "+" overloading
const Integer Integer::operator+(const Integer &secondNumber)
{
Integer temp = this->intValue + secondNumber.intValue;
return temp;
}
// operator "=" overloading
void Integer::display(std::ostream& stream) const
{
stream << this->intValue;
}
// comparasion operator overload
bool Integer::operator==(const Integer& rhs) const
{
return this->intValue == rhs.intValue;
}
Fraction.h
#pragma once
#include "Number.h"
#include "Integer.h"
class Fraction : public Number<Fraction>
{
Integer _numerator;
Integer _denominator;
public:
void display(std::ostream &) const;
Fraction() = delete;
Fraction(const int &, const int &);
const Fraction operator+ (const Fraction &);
int gcdCalculate(int val1, int val2);
int lcmCalculate(const int val1, const int val2);
virtual ~Fraction() {}
bool operator==(const Fraction& rhs) const;
};
Fraction.cpp
#include "Fraction.h"
#include <iostream>
// parameterised constructor
Fraction::Fraction(const int & num, const int & den)
{
_numerator.setValue(num);
_denominator.setValue(den);
}
// display the fraction value
void Fraction::display(std::ostream & stream) const
{
if (this->_denominator == 0)
std::cout << "Undefined: " << this->_numerator.getValue() << "/" << this->_denominator.getValue() << " (Divide By Zero Exception)";
else
stream << this->_numerator.getValue() << "/" << this->_denominator.getValue();
}
// "+" operator overloading
const Fraction Fraction::operator+(const Fraction &numberTwo)
{
int lcm = lcmCalculate(this->_denominator.getValue(), numberTwo._denominator.getValue());
int multiplier1 = 0;
if (this->_denominator.getValue())
multiplier1 = lcm / this->_denominator.getValue();
int multiplier2 = 0;
if (numberTwo._denominator.getValue())
multiplier2 = lcm / numberTwo._denominator.getValue();
return Fraction((this->_numerator.getValue() * multiplier1) + (numberTwo._numerator.getValue() * multiplier2), lcm);
}
// LCM Calculation
int Fraction::lcmCalculate(const int val1, const int val2)
{
int temp = gcdCalculate(val1, val2);
return temp ? (val1 / temp * val2) : 0;
}
// GCD Calculation
int Fraction::gcdCalculate(int val1, int val2)
{
for (;;)
{
if (val1 == 0) return val2;
val2 %= val1;
if (val2 == 0) return val1;
val1 %= val2;
}
}
// comparision operator overload
bool Fraction::operator==(const Fraction& rhs) const
{
Integer numCheck = this->_numerator;
Integer denCheck = this->_denominator;
if (rhs._numerator.getValue())
numCheck.setValue(numCheck.getValue() / rhs._numerator.getValue());
if (rhs._numerator.getValue())
denCheck.setValue(denCheck.getValue() / rhs._denominator.getValue());
if (numCheck == denCheck) {
return true;
}
return false;
}
QUESTION:
I am confused as how to add Integer + Fraction class.
Do I need to create another class which will inherit from Number class.
How to overload oprator+ present in Number Class.
Suppose I try to add Integer + Fraction = Fraction in the Integer class itself then I will have something like
Example
class Integer : public Number<Integer>
{
const Fraction operator+(const Fraction &);
}
const Fraction Integer::operator+(const Fraction &numberTwo)
{
^^ I will get error here
// Addition opeartion
}
Please help me.
For your first question, the solution is to not use member function overloads, but to create a non-member function overload, e.g.
Fraction operator+(Integer const& integer, Fraction const& fraction)
{
// Logic to add the integer and fraction here
// Perhaps something like...
Fraction f(integer.getValue(), 1); // Create fraction
return f + fraction;
}
The code above uses the Fraction::operator+ function to add the integer.
While you can add an Integer+Fraction operator to your current design like Joachim suggested, that's going to result in some code duplication or at least some unnecessary boilerplate.
I suggest an alternative design instead: Make Integer convertible to Fraction. After all, any integer can be represented by the Fraction type, right?
You can make Integer convertible in two ways: By adding a conversion function to Integer, or by adding a converting constructor to Fraction.
I recommend choosing the converting constructor approach, because Fraction already depends on Integer type and so the cast operator would result in a circular dependency, just like your member operator attempt. I'll leave the implementation as an exercise.
This design requires that the addition operator is implemented as a non-member overload:
Fraction operator+(Fraction const& left, Fraction const& right)
With the converting constructor, this function can handle any combination of (F + F), (F + I) and (I + F).
I'm currently trying to create a member function that adds an object, Fraction f, to the current object and returns a reference to the current object. my second function is a non-friend helper operator that adds two Fraction objects and returns a copy of the result. I'm not exactly sure how to go about doing this and was looking for some advice. Pretty much the object(s) are just fractions that have been simplified earlier within a previous member function. Essentially all I'm doing is adding simplified fractions. Here's what I have so far:
//header.h
class Fraction {
int num;
int den;
public:
Fraction();
Fraction(int, int);
Fraction& operator+=(const Fraction& f);
friend bool operator==(const Fraction&, const Fraction&);
void simplify();
void display() const;
};
Fraction operator+(const Fraction&, const Fraction&);
and the module:
//module.cpp
#include "Fraction.h"
#include <iostream>
Fraction::Fraction() {
num = 0;
den = 0;
}
Fraction::Fraction(int n, int d) {
num = n;
den = d;
simplify();
}
void Fraction::simplify() {
int temp = den;
int a = num;
int b = den;
int gcd;
if (b > a) {
b = num;
a = den;
}
while (temp != 0) {
temp = a % b;
a = b;
b = temp;
}
gcd = a;
num /= gcd;
den /= gcd;
}
void Fraction::display() const {
std::cout << num << "/" << den << std::endl;
}
//member function in question
Fraction& Fraction::operator+=(const Fraction& f) {
num += f.num;
den += f.den;
return *this;
}
//member function in question
Fraction operator+(const Fraction&, const Fraction&) {
}
EDIT: guess I wasn't as clear previously and that's partly due to the helper function not being revealed. I tried defining the member function and the above code is currently what I have at the moment. I'm not sure if it's logically sound or not as I am still going through the other definitions. the non friend helper operator is the one I am stumped on and do not know what to do. If I can get some help on whether the definition I have for the += member operator is correct and some advice on how to approach the non friend helper operator, that would be great. sorry for any confusion.
since you have the fractions simplified, all you have to do is to use this equation:
and the code will be as follows:
Fraction& Fraction::operator+=(const Fraction& f) {
num = num * f.den + f.num * den;
den *= f.den;
simplify();
return * this;
}
EDIT:
take a look at this question for more about operator overloading