重载输入cin和输出cout运算符

1.C++中重载输入cin和输出cout运算符

  • 在C++中,标准库本身已经对左移运算符<<和右移运算符>>分别进行了重载,使其能够用于不同数据的输入输出,但是输入输出的对象只能是 C++内置的数据类型(例如 bool、int、double 等)和标准库所包含的类类型(例如 string、complex、ofstream、ifstream 等)。如果自己定义了一种新的数据类型,需要用输入输出运算符去处理,那么就必须对它们进行重载。本节以complex类为例来演示输入输出运算符的重载。
  • 本节要达到的目标是让复数的输入输出和int、float等基本类型一样简单。假设num1、num2是复数:
        cout << num1 << num2 << endl;  // 输出形式
        cin >> num1 >> num2 << endl;   // 输入形式
    
  • cout是ostream类的对象,cin是istream类的对象,要想达到这个目标,就必须以全局函数(友元函数)的形式重载<<和>>,否则就要修改标准库中的类,这显然不是我们所期望的

2.重载输入运算符>>

  • 以全局函数的形式重载>>,使它能够读入两个double类型的数据,并分别赋值给复数的实部和虚部:
        istream & operator>> (istream &in, complex &A);{
            in >> A.real >> A.imag;
            return in;
        }
    
  • istream表示输入流,cin是istream类的对象,只不过这个对象是在标准库中定义的。之所以返回 istream类对象的引用,是为了能够连续读取复数,让代码书写更加漂亮,例如:
        complex c1, c2;
        cin>>c1>>c2;
    
  • 如果不返回引用,那就只能一个一个地读取了:
        complex c1, c2;
        cin>>c1;
        cin>>c2;
    
  • 注意:运算符重载函数中用到了complex类的private 成员变量,必须在complex类中将该函数声明为友元函数,如下例所示:
        friend istream & operator>> (istream &in, complex &A);
    

3.重载输出运算符<<

  • 同样地,也可以模仿上面的形式对输出运算符>>进行重载,让它能够输出复数,请看下面的代码:
        ostream & operator<< (ostream &out, complex &A){
            out << A.real << "+" << A.imag << "i" << endl;
            return out;
        }
    
  • ostream表示输出流,cout是ostream类的对象。由于采用了引用的方式进行参数传递,并且也返回了对象的引用,所以重载后的运算符可以实现连续输出。为了能够直接访问complex类的private成员变量,同样需要将该函数声明为complex类的友元函数:
        friend ostream & operator<< (ostream &out, complex &A);
    

4.综合实例如下:

```
    #include "iostream"

    using namespace std;

    class complex
    {
    public:
        complex(double a = 0.0, double b = 0.0) : real(a), img(b){};

    public:
        friend complex operator+(const complex &A, const complex &B);
        friend complex operator-(const complex &A, const complex &B);
        friend complex operator*(const complex &A, const complex &B);
        friend complex operator/(const complex &A, const complex &B);
        friend istream &operator>>(istream &in, complex &A);
        friend ostream &operator<<(ostream &out, complex &A);

    private:
        double real;
        double img;
    };

    // 重载加法运算符
    complex operator+(const complex &A, const complex &B)
    {
        complex C;
        C.real = A.real + B.real;
        C.img = A.img + B.img;
        return C;
    }
    // 重载减法运算符
    complex operator-(const complex &A, const complex &B)
    {
        complex C;
        C.real = A.real - B.real;
        C.img = A.img - B.img;
        return C;
    }

    // 重载乘法运算符
    complex operator*(const complex &A, const complex &B)
    {
        complex C;
        C.real = A.real * B.real - A.img * B.img;
        C.img = A.img * B.real + A.real * B.img;
        return C;
    }

    // 重载除法运算符
    complex operator/(const complex &A, const complex &B)
    {
        complex C;
        double square = A.real * A.real + A.img * A.img;
        C.real = (A.real * B.real + A.img * B.img) / square;
        C.img = (A.img * B.real - A.real * B.img) / square;
        return C;
    }

    // 重载输入运算符
    istream &operator>>(istream &in, complex &A)
    {
        in >> A.real >> A.img;
        return in;
    }

    // 重载输出运算符
    ostream &operator<<(ostream &out, complex &A)
    {
        out << A.real << " + " << A.img << "i";
        return out;
    }

    int main()
    {
        complex c1, c2, c3;
        cin >> c1 >> c2;

        c3 = c1 + c2;
        cout << "c1 + c2 = " << c3 << endl;

        c3 = c1 - c2;
        cout << "c1 - c2 = " << c3 << endl;

        c3 = c1 * c2;
        cout << "c1 * c2 = " << c3 << endl;

        c3 = c1 / c2;
        cout << "c1 / c2 = " << c3 << endl;

        return 0;
    }
```
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
输入输出运算符重载是C++中的一种特性,它允许我们自定义类对象在输入输出流中的行为。通过重载输入输出运算符,我们可以实现自定义的对象输入输出格式。 在C++中,输入输出运算符重载使用友元函数来实现。常用的输入输出运算符包括插入运算符(<<)和提取运算符(>>)。 对于输出运算符(<<),我们可以通过重载运算符来定义对象在输出流中的输出格式。例如,我们可以重载运算符输出对象的成员变量值。 对于输入运算符(>>),我们可以通过重载运算符来定义对象在输入流中的输入格式。例如,我们可以重载运算符来接收用户输入,并将输入的值赋给对象的成员变量。 下面是一个示例代码,演示了如何重载输入输出运算符: ```cpp #include <iostream> class MyClass { private: int value; public: MyClass(int v) : value(v) {} // 重载插入运算符(<<) friend std::ostream& operator<<(std::ostream& os, const MyClass& obj) { os << "Value: " << obj.value; return os; } // 重载提取运算符(>>) friend std::istream& operator>>(std::istream& is, MyClass& obj) { is >> obj.value; return is; } }; int main() { MyClass obj(0); std::cout << "请输入一个整数: "; std::cin >> obj; std::cout << "输入的值为: " << obj << std::endl; return 0; } ``` 在上述示例中,我们定义了一个名为MyClass的类,其中包含一个私有成员变量value。通过重载插入运算符(<<)和提取运算符(>>),我们可以自定义MyClass对象在输入输出流中的行为。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值