重载赋值操作/流

时间:2012-12-12 15:48:21

标签: c++ operator-overloading overloading

我想知道以下用于对有理数进行操作的代码是否实际上已经重载了赋值操作和流插入运算符<<打印对象。我对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;
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:2)

简答:是的,它适用于<<和算术运算。 它不适合作业。

例如,这是<<重载:

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

要重载assignment operator,您应该编写一个函数

rational& rational::operator=(const rational &o) {
    this->den = o.den;
    this->num = o.num;
    return *this;
}

答案 2 :(得分:0)

插入和分配都很好。赋值运算符 (和复制构造函数)由编译器提供 在这种情况下很好。如果由于某种原因你确实想提供 赋值运算符(或者你需要其他类), 你几乎肯定需要提供一个拷贝构造函数, 而且大部分时间都是析构函数。但事实并非如此 这里;编译器生成的版本很好(最重要的是 我认为C ++程序员会期望。

关于代码的其他评论:你的方式 写了构造函数,插入操作符就是 很好,并且优于建议的替代方案。