类重载运算符的功能

时间:2015-07-23 20:21:26

标签: c++ class oop operator-overloading

在下面的代码中,我遇到了名为normalize的函数问题。基本上在这个程序中,我输入一个分数为2个整数。如果输入为负数,则该归一化函数应该使分母为正,并且使分子和/或如果两者都输入为负数,则分母为正。它也应该将分数转换为最低项形式。所以这适用于一些输入而不是其他输入,我不明白为什么。

同样在main函数中,接近结束时我尝试测试这个规范化函数,我不知道如何使用oop原则来做到这一点。我是通过直接引用成员变量来做到的。我怎么能这样做呢?

这是我的课程代码:

class Rational
{
public:
    Rational(int numer, int denom);//Initialize an object with 1 arguement for numerator and 1 for denominator
    Rational(int wholeNumber);//Initialize an object with 1 argument for numerator. Sets denominator to 1.
    Rational();//Initialize an object with 0 for numerator and 1 for denominator
    friend ostream& operator <<(ostream& outputStream, const Rational& number);//Overloading of insertion operator
    friend istream& operator >>(istream& inputStream, Rational& number);//Overloading of extraction operator
    friend bool operator ==(const Rational& number1, const Rational& number2);//Overloading of boolean "=="
    friend bool operator <(const Rational& number1, const Rational& number2);//Overloading of inequality sign
    friend bool operator <=(const Rational& number1, const Rational& number2);//Overloading of inequality sign
    friend bool operator >(const Rational& number1, const Rational& number2);//Overloading of inequality sign
    friend bool operator >=(const Rational numerator, const Rational& denominator);//Overloading of inequality sign
    friend const Rational operator +(const Rational& number1, const Rational& number2);//Overloading of + operator
    friend const Rational operator -(const Rational& number1, const Rational& number2);//Overloading of - operator
    friend const Rational operator *(const Rational number1, const Rational& number2);//Overloading of * operator
    friend const Rational operator /(const Rational& number1, const Rational& number2);//Overloading of / operator
    Rational normalize();//Normalizes the number so fraction is in lowest terms and denominator is positive
    int calcGCD(int numerator, int denominator);//Finds greatest common divisor between numerator and denominator
private:
    int numerator, denominator;
    };

这是规范化功能:

Rational Rational::normalize()
{
    if ((numerator > 0 && denominator < 0) || (numerator < 0 && denominator < 0))
    {
        denominator = -denominator;
        numerator = -numerator;
    }

    int gcd = calcGCD(numerator, denominator);
    numerator = numerator/gcd;
    denominator = denominator/gcd;

    return Rational(numerator, denominator);
}

1 个答案:

答案 0 :(得分:1)

以下是一些处理规范化的简单方法。注意除了删除咆哮者之外,我还没有真正检查OP的逻辑。它足以回答OP关于如何在OO环境中正确使用它的问题。

1自由浮动功能

此选项将reference传递给Rational,并创建并返回不同的Rational

Rational normalize(const Rational & in)
{
    // get values from Rational to work on so we don't have to keep getting  
    // them over and over
    int num = in.getNumerator(); 
    int den = in.getDenominator();
        // numerator and denominator are public, so getters aren't strictly necessary,
        // but data members should always be private unless given a damn good reason 
        // and probably not even then.  If you're going to learn, might as well learn 
        // to do it right, go private and use getters.

    if (den < 0)
    { // if the denominator is negative, flip the signs of both den and num
        den *= -1;
        num *= -1;
    }

    int gcd = calcgcd(num, den); //grabbing gcd and string in temp
    // note I changed the name of the function. int gcd = gcd() is in poor taste even 
    // if it does compile, and the function should state what it does. 
    // It doesn't GCD, it calculates GCD.
    num = num / gcd;
    den = den / gcd;

    return Rational(num, den); // create and return a new Rational. 
                               // Compiler should be smart enough to avoid copying 
}

使用它:

Rational notNormalized(245, 49);
Rational normalized = normalize(notNormalized);

2非破坏性方法

在这个变体中,Rational使用自己来创建一个不同的,规范化的Rational。

因为这是一个类方法,所以需要将其声明添加到类中。

Rational normalize() const;

const标记表示使用此方法不会以任何方式更改Rational对象。尝试更改normalize方法中的对象将导致编译器错误。

然后执行:

Rational Rational::normalize() const
{
    // get copies of numerator and denominator so we don't damage this Rational  
    int num = numerator;
    int den = denominator;
    if (den < 0)
    { 
        den *= -1;
        num *= -1;
    }

    int gcd = calcgcd(num, den);
    num = num / gcd;
    den = den / gcd;

    return Rational(num, den); 
}

使用它:

Rational notNormalized(245, 49);
Rational normalized = notNormalized.normalize();

3修改方法

在此变体中,Rational规范化自身

因为这是一个类方法,所以需要将其声明添加到类中。

Rational normalize();

然后执行:

void Rational::normalize()
{
    if (denominator < 0)
    { 
        denominator *= -1;
        numerator *= -1;
    }
    int gcd = calcgcd(numerator, denominator);
    numerator /= gcd;
    denominator /= gcd;
}

使用它:

Rational soonToBeNormalized(245, 49);
soonToBeNormalized.normalize();

附录

要快速回答问题,请将问题降至最低限度。类定义,规范化函数和&#34;如何使这个规范化函数与这个Rational类一起工作?&#34;本来就足够了。其余的是糠,吸引了人们的兴趣,并将帮助人们。

相关问题