1

我想知道以下用于对有理数进行操作的代码是否实际上已经重载了赋值操作和流插入操作符 << 以打印对象。我对 C++ 不是很好,所以这对我来说是新的,但据我所知,我认为它已经做到了。

    /*
 *
 *  C++ version
 *
 */

/* rational.h */

#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>

using std::ostream;

struct rational {

    rational(int = 0, int = 1);

    rational operator+(const rational &) const;
    rational operator-(const rational &) const;
    rational operator*(const rational &) const;
    rational operator/(const rational &) const;

    rational operator+(int) const;
    rational operator-(int) const;
    rational operator*(int) const;
    rational operator/(int) const;

    friend rational operator+(int, const rational &);
    friend rational operator-(int, const rational &);
    friend rational operator*(int, const rational &);
    friend rational operator/(int, const rational &);

    friend ostream &operator<<(ostream &, const rational &);

private:

    int den;
    int num;
};

#endif /* RATIONAL_H */

/* rational.cc */

#include <iostream>
#include "rational.h"

rational::rational(int num, int den) : num(num), den(den) {}

rational rational::operator+(const rational &o) const {

    return rational(num * o.den + o.num * den, den * o.den);
}

rational rational::operator+(int n) const {

    return rational(num + n * den, den);
}

rational rational::operator-(const rational &o) const {

    return rational(num * o.den - o.num * den, den * o.den);
}

rational rational::operator-(int n) const {

    return rational(num - n * den, den);
}

rational rational::operator*(const rational &o) const {

    return rational(num * o.num, den * o.den);
}

rational rational::operator*(int n) const {

    return rational(num * n, den);
}

rational rational::operator/(const rational &o) const {

    return rational(num * o.den, den * o.num);
}

rational rational::operator/(int n) const {

    return rational(num, den * n);
}

rational operator+(int n, const rational &o) {

    return o + n;
}

rational operator-(int n, const rational &o) {

    return rational(n) - o;
}

rational operator*(int n, const rational &o) {

    return o * n;
}

rational operator/(int n, const rational &o) {

    return rational(n) / o;
}

ostream &operator<<(ostream &out, const rational &o) {

    out << '(' << o.num << " / " << o.den << ')';
    return out;
}

/* main.cc */

#include <iostream>
#include "rational.h"

using std::cout;
using std::endl;

int main(void) {

    rational a(1, 2);
    rational b(2, 3);

    int i = 5;

    cout << a << " + " << b << " = " << a + b << endl;
    cout << a << " - " << b << " = " << a - b << endl;
    cout << a << " * " << b << " = " << a * b << endl;
    cout << a << " / " << b << " = " << a / b << endl;

    cout << a << " + " << i << " = " << a + i << endl;
    cout << a << " - " << i << " = " << a - i << endl;
    cout << a << " * " << i << " = " << a * i << endl;
    cout << a << " / " << i << " = " << a / i << endl;

    cout << i << " + " << a << " = " << i + a << endl;
    cout << i << " - " << a << " = " << i - a << endl;
    cout << i << " * " << a << " = " << i * a << endl;
    cout << i << " / " << a << " = " << i / a << endl;

    return 0;
}
4

3 回答 3

2

没关系(你可以写在operator<<一行中,但在这里没关系):

ostream &operator<<(ostream &out, const rational &o) {
    return out << '(' << o.num << " / " << o.den << ')';
}

但是,您还没有定义赋值运算符!

rational & rational::operator=(rational const &rhs) {
    den = rhs.den;
    num = rhs.num;
    return *this;
}
于 2012-12-12T15:51:25.707 回答
2

简短的回答:是的,它适用于<<和算术运算。它不是为了分配。

例如,这是<<重载:

ostream &operator<<(ostream &out, const rational &o) {
    out << '(' << o.num << " / " << o.den << ')';
    return out;
}

要重载赋值运算符,您应该编写一个函数

rational& rational::operator=(const rational &o) {
    this->den = o.den;
    this->num = o.num;
    return *this;
}
于 2012-12-12T15:51:52.170 回答
0

插入和分配都很好。赋值运算符(和复制构造函数)由编译器提供,在这种情况下很好。如果出于某种原因您确实想提供一个赋值运算符(或者您需要为其他一些类提供),那么您几乎可以肯定也需要提供一个复制构造函数,而且大多数情况下是一个析构函数。但这里不是这样。编译器生成的版本很好(我认为这是大多数 C++ 程序员所期望的)。

关于您的代码的其他评论:您编写构造函数和插入运算符的方式很好,并且比建议的替代方案更可取。

于 2012-12-12T16:53:06.737 回答