【问题标题】:overloading operators and friend functions重载运算符和友元函数
【发布时间】:2017-04-18 22:00:48
【问题描述】:

我在使用重载运算符实现友元函数时遇到问题。我对使用这些功能的目的没有深入的了解。如果有人可以给我一些关于如何进行的指导。请提前感谢您。 说明:

This class implements rational number of the type 2/3.

class Rational;

private data: int n, (fraction numerator)
int d (fraction denominator).

//   make sure that d is always positive.

//  optional:   always simplify the number so that n and d don't have a common factor


public interface:
constructors:
two int args, to allow setting rational to any legal value
default numerator to be 0, default denominator to be 1

friend istream& operator >>(istream& in_str, Rational& right)

friend ostream& operator <<(ostream& out_str, cnst Rational& right)



overload + - * / <  <=  > >=   ==

friend Rational operator+(const Rational&, const Rational&);
....


*/ 
/*
A possible test and output is given below.

int main()
{
cout << "Testing declarations" << endl;
cout << "Rational x, y(2), z(-5,-6), w(1,-3);" << endl;
Rational x, y(2), z(-5, -6), w(1, -3);
cout << "x = " << x << ", y = " << y << ",  z = " << z
<< ", w = " << w << endl << endl;



cout << "Testing the constructor and normalization routines: " << endl;
y = Rational(-1, -4);
cout << "y =Rational(-1, -4) outputs as " << y << endl;
y = Rational(-1, 4);
cout << "y =Rational(-1, 4)outputs as " << y << endl;
y = Rational(1, -4);
cout << "y = Rational(128, -48) outputs as " << y << endl;
Rational a(1, 1);
cout << "Rational a(1,1); a outputs as: " << a << endl;
Rational ww = y*a;
cout << y << " * " << a << " = " << ww << endl << endl;

cout << "Testing >> overloading: \nEnter "
<< "a fraction in the format "
<< "integer_numerator/integer_denominator"
<< endl;
cin >> x;
cout << "You entered the equivalent of: " << x << endl;
cout << z << " -  (" << w << ") = " << z - w << endl << endl;



w = Rational(-7, 3);
z = Rational(-3, 5);
cout << "Testing arithmetic and relational "
<< " operator overloading" << endl << endl;
cout << w << " * " << z << " = " << w * z << endl;
cout << w << " + " << z << " = " << w + z << endl;
cout << w << " - " << z << " = " << w - z << endl;
cout << w << " / " << z << " = " << w / z << endl;

cout << w << " <  " << z << " : " << (w < z) << endl;

cout << w << " <= " << z << " : " << (w <= z) << endl;
cout << w << " <= " << w << " : " << (w <= w) << endl;

cout << w << " >  " << z << " : " << (w > z) << endl;
cout << w << " > " << w << " : " << (w > w) << endl;
cout << w << " >= " << z << " : " << (w >= z) << endl;

return 0;
}

== == == == == == == == ==

TestingTesting declarations
Rational x, y(2), z(-5, -6), w(1, -3);
x = 0 / 1, y = 2 / 1, z = 5 / 6, w = -1 / 3

Testing the constructor and normalization routines :
y = Rational(-1, -4) outputs as 1 / 4
y = Rational(-1, 4)outputs as - 1 / 4
y = Rational(128, -48) outputs as - 1 / 4
Rational a(1, 1); a outputs as : 1 / 1
- 1 / 4 * 1 / 1 = -1 / 4

Testing >> overloading :
    Enter a fraction in the format integer_numerator / integer_denominator
    2 / -3
    You entered the equivalent of : -2 / 3
    5 / 6 - (-1 / 3) = 21 / 18

    Testing arithmetic and relational  operator overloading

    - 7 / 3 * -3 / 5 = 21 / 15
    - 7 / 3 + -3 / 5 = -44 / 15
    - 7 / 3 - -3 / 5 = -26 / 15
    - 7 / 3 / -3 / 5 = 35 / 9
    - 7 / 3 <  -3 / 5 : 1
    - 7 / 3 <= -3 / 5 : 1
    - 7 / 3 <= -7 / 3 : 1
    - 7 / 3 >  -3 / 5 : 0
    - 7 / 3 > -7 / 3 : 0
    - 7 / 3 >= -3 / 5 : 0

*/

我的代码:

#include <iostream>
using namespace std;

class Rational {
    private:
        int n; // (fraction numerator)
        int d; // (fraction denominator).

    public:
        Rational() {
            n = 0;
            d = 1;
        }
        Rational(int num, int denom) {
            n = num;
            d = denom;
        }

        friend istream& operator >> (istream& in_str, Rational& right);
        friend ostream& operator << (ostream& out_str, const Rational& right);
        friend Rational operator+ (const Rational&, const Rational&);

        //friend istream& operator >> (istream& in_str, Rational& right);
        //friend ostream& operator <<(ostream& out_str, const Rational& right);
        //friend Rational operator+(const Rational&, const Rational&);


};




int main()
{
    cout << "Testing declarations" << endl;
    cout << "Rational x, y(2), z(-5,-6), w(1,-3);" << endl;
    return 0;
}

istream& operator >> (istream& in_str, Rational& right) {
    cin >> n.right;
}
ostream& operator << (ostream& out_str, const Rational& right); {
    cout << out_str;
}
Rational operator+(const Rational&, const Rational&); {
    cout << Rational;
}

【问题讨论】:

  • “我在使用重载运算符实现友元函数时遇到问题” - 有什么问题?抱歉……我们不是这里的魔术师。 :-)。请看How to ask
  • 我建议阅读一本好的教科书并阅读示例。从长远来看,这比获得这些特定问题的答案对您更有帮助。

标签: c++ operator-overloading friend-function


【解决方案1】:

这样想……在您的示例中,您并没有真正重载类中的方法,而是为流重载了运算符>>。 你必须让它成为一个朋友以便执行

istream& operator >> (istream& in_str, Rational& right)

和其他流操作符可以看到你类的成员数据。它们不是您的类的方法,而是被定义为全局函数。如果他们不是朋友函数,他们就看不到他们需要处理的私有数据。

https://isocpp.org/wiki/faq/friendshttps://isocpp.org/wiki/faq/operator-overloading

这是使用关键字“朋友”可以接受的极少数情况之一。 “朋友”通常在所有情况下都不受欢迎,但在实现工厂模式时除外。

对比一下,google“C++重载运算符=”是一个类方法,因此不需要加好友。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-19
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多