美文网首页
C++输出输出重载

C++输出输出重载

作者: ebayboy | 来源:发表于2019-09-30 09:27 被阅读0次

    /* class_opt_cin_out overload */

    #include <iostream>

    using namespace std;

    class Distance {

    private:

    int feets;

    int inches;

    public:

    Distance() {

    this->feets = 0;

    this->inches = 0;

    }

    Distance(int f, int i) {

    this->feets = f;

    this->inches = i;

    }

    friend ostream &operator<< (ostream &output, const Distance &D) {

    output << "F:" << D.feets << " I: " << D.inches;

    return output;

    }

    friend istream &operator>>(istream &input, Distance &D) {

    input >> D.feets >> D.inches;

    return input;

    }

    };

    int main() {

    Distance D1(11, 10);

    Distance D2(5,11);

    Distance D3;

    cin >> D3;

    cout << "D1:" << D1 << endl;

    cout << "D2:" << D2 << endl;

    cout << "D3:" << D3 << endl;

    return 0;

    }

    相关文章

      网友评论

          本文标题:C++输出输出重载

          本文链接:https://www.haomeiwen.com/subject/qoufuctx.html