Home > Back-end >  Calling private method from a Public method in the same class
Calling private method from a Public method in the same class

Time:03-23

I am trying to call the method gcd() in my multiplication method but I'm not sure what the correct way is. When I run the code below the console displays blank lines, when it should print a fraction. I've tried calling this->gcd(), fraction2.gcd() and gcd().

EDIT: Added cout override and main() to be able to run

#include <iostream>
using namespace std;

class Fraction {
    friend ostream & operator<<(ostream & os, Fraction fraction) {
        os << " " << fraction.numerator << "\n" << "---" << "\n" << " " << fraction.denominator;

        return os;
    }

    friend istream & operator>>(istream &, Fraction);

private:
    int numerator;
    int denominator;

    int gcd(int num1, int num2) {
        int absNum1 = abs(num1);
        int absNum2 = abs(num2);

        while (absNum2 != 0) {
            int remainder = absNum1 % absNum2;
            absNum1 = absNum2;
            absNum2 = remainder;
        }

        return absNum2;
    };

public:
    Fraction(int n, int d) {
        this->denominator = d;
        this->numerator = n;
    }

    Fraction(int n) {
        this->numerator = n;
        this->denominator = 1;
    }

    Fraction() {
        this->numerator = 1;
        this->denominator = 1;
    }

    Fraction operator*(Fraction fraction2) const {
        Fraction multipliedFraction = Fraction();

        multipliedFraction.numerator = this->numerator * fraction2.denominator;
        multipliedFraction.denominator = this->denominator * fraction2.numerator;

        int fractionGCD = this->gcd(multipliedFraction.numerator, multipliedFraction.denominator);
        multipliedFraction.numerator = multipliedFraction.numerator / fractionGCD;
        multipliedFraction.denominator = multipliedFraction.denominator / fractionGCD;

        return multipliedFraction;
    }


};

int main() {
    Fraction fraction1 = Fraction(1, 2);
    Fraction fraction2 = Fraction(1, 2);

    fraction2 = fraction1 * fraction2;

    cout << fraction2;

    return 0;
}

CodePudding user response:

ok here goes

why this->gcd doesnt work. YOu have

class Fraction {
    int gcd(int num1, int num2) {
        int absNum1 = abs(num1);
        int absNum2 = abs(num2);

        while (num2 != 0) {
            int remainder = absNum1 % absNum2;
            absNum1 = absNum2;
            absNum2 = remainder;
        }

        return absNum2;
    };

public:
     Fraction &operator*(const Fraction &fraction2) const {
        ....
    }

in operator* this is const - it says so on the end of the statement. So you cannot invoke a non-const method on it.

gcd is a non const method.

This can be fixed by

 int gcd(int num1, int num2) const {

It compiles now

But as other have pointed out this does not need to be a member function. What does it even mean to say frac.gcd(a,b), the method doesnt even look at frac.

You want

 static int gcd(int num1, int num2) {

now you can call it like this

int gcd = Fraction::gcd(multipliedFraction.numerator, multipliedFraction.denominator);

Note that I changed the calling signature of your operator* to be

 Fraction operator*(const Fraction &fraction2) const

this is what it should be as per

 https://gist.github.com/beached/38a4ae52fcadfab68cb6de05403fa393

CodePudding user response:

The issue was that gcd was not const which didn't allow me to use this->gcd and gcd function was returning 0 resulting in a divide by 0 error. Switching my gcd function to this worked

int gcd(int num1, int num2) const {
        int absNum1 = abs(num1);
        int absNum2 = abs(num2);

        while (absNum2 != 0) {
            int remainder = absNum1 % absNum2;
            absNum1 = absNum2;
            absNum2 = remainder;
        }

        return absNum1;
    };
  •  Tags:  
  • c
  • Related