Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Performance Engineering 10th European Workshop Epew 2013 Venice Italy September 17 2013 Proceedings

Authors: Maria Simonetta Balsamo ,William Knottenbelt ,Andrea Marin

2013 Edition

3642407242, 978-3642407246

More Books

Students also viewed these Programming questions

Question

Show enthusiasm for the position (but not too much).

Answered: 1 week ago