Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Operator Overloading in C++ ?I have already done the overloading for + and - will be similar. Ineed help for overloading *, /, ==, ,
Operator Overloading in C++
?I have already done the overloading for + and - will be similar. Ineed help for overloading *, /, ==, <, >, <=, and>= for the given Fraction class.
The definition of the Fraction class, member functions, aswell as the complete overload of the + operator is provided in theattached .cpp file.
?From what I am understanding, the major changes must be done in //Operator + overload: Definition
const Fraction Fraction::operator +(const Fraction& other)const section..
Language: C++
// OperatorOverLoading.cpp #include "stdafx.h"#include#include using namespace std;class Fraction{private: int num; int den; /*If gcd() is non static you cannot call it inside operator definition. If you want gcd() be non static. you can take out const qualfier of the operator. */ int static gcd(int, int);public: Fraction(); Fraction(int, int); void setFraction(int, int); //Operator + overload: Prototype const Fraction operator +(const Fraction&) const; string getString();};//Constructor definitionsFraction::Fraction(){}Fraction::Fraction(int _num, int _den){ setFraction(_num, _den);}//Other function definitionsvoid Fraction::setFraction(int _num, int _den){ if (_den == 0) { num = 0; den = 1; } else { num = _num; den = _den; }}int Fraction::gcd(int a, int b){ if (a == 0) return b; else if (b == 0) return a; int temp; while (b != 0) { temp = b; b = a%b; a = temp; } return a;}string Fraction::getString(){ return to_string(num) + "/" + to_string(den);}//Operator + overload: Definitionconst Fraction Fraction::operator +(const Fraction& other) const{ int n, d, g; n = num*other.den + den*other.num; d = den*other.den; g = gcd(n, d); return Fraction(n / g, d / g);}int main(){ Fraction f1(1, 3); Fraction f2(7, 6); Fraction f = f1+f2; cout << f1.getString() << "+" << f2.getString() << "=" << f.getString() << endl; for (;;); return 0;}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started