c++中>>和<<的重载

我在学习c++输入输出的重载的一些心得,给大家分享一下,有问题的地方欢迎大家指出来~

先上代码:

#include<iostream>
using namespace std;

class Complex{
private:
    int  id;
    int real;
    int image;
public:
    Complex(int _real=0, int _image=0, int _id=0); // 构造函数
    ~Complex(); // 析构函数
    friend ostream& operator<<(ostream &out, const Complex& c);
    friend istream& operator>>(istream &in, Complex &c);
};

Complex::Complex(int _real, int _image, int _id) : real(_real), image(_image), id(_id) {}

Complex::~Complex() {
    cout << "Destroyed." << endl;
}

ostream& operator<<(ostream &out, const Complex& c) {
    out << c.id << " " << "Complex:" << c.real << "+" << c.image << "i" << endl;
    return out;
}

istream& operator>>(istream &in, Complex &c) {
    in >> c.id >> c.real >> c.image;
    return in;
}

int main() {
    Complex a,b;
    cout << a;
    cout << "分割线" << endl;
    cin >>b ;
    cout << b;
    return 0;
}

现在进行说明:

当我们在c++中创建了一个类后,通常需要通过成员函数来打印类中的数据成员,如上面的Complex类

class Complex{
private:
    int  id;
    int real;
    int image;
}

现在定义一个Complex a;

直接cout<<a;

显然报错,因为a是一个类,不是c++输入输出流能够输出的数据类型,所以我们需要对输入输出符进行重载,通常对>>,<<的重载我们以友元函数来进行,这是因为这样编写代码更符合我们的阅读习惯,如果是通过成员函数来进行重载,调用函数的时候需要如下

 
a<<cout<<endl;

显然不符合我们的编写习惯;

输出重载的形式式如下

friend ostream& operator<<(ostream &out, const Complex& c) { out<<c.real; return out; }

输入重载的形式如下

friend istream& operator>>(istream &in, Complex &c) { in>>c.real; return in; }

上面两个都是以本文的Complex类为例子的,函数的第一个参数是输入或者输出流的引用,第二个参数是类的引用,这是因为 istream 类的对象不允许被复制。istream 类的设计原则是每个输入流对象都应该是独一无二的,因为它们通常与设备或文件相关联,复制这些对象可能会导致资源管理上的问题。因此,istream 类的拷贝构造函数和拷贝赋值运算符都是删除的(private且未定义),这意味着你不能创建 istream 对象的副本。

使用引用作为 operator>> 的参数,可以避免复制 istream 对象,同时允许你的函数直接操作传入的输入流对象。此外,使用引用还有助于保持性能,因为它避免了不必要的数据复制。

operator>> 函数返回 istream & 时,它通常返回对输入流对象的引用,这样可以支持链式输入操作。链式输入操作允许你在一行代码中连续调用多个输入操作符,例如 cin >> x >> y >> z;,其中每个 >> 操作符都会返回它操作的输入流对象的引用,从而允许下一个 >> 操作符继续在同一个输入流上进行操作

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们在C++使用`>>`和`<<`运符时,通常及到输入和输出流。这些运算用于输入和输出数据到流。 下面是一些关于`>>`和`<<`的使用示例: 1. 输出数据到控制台: ```cpp #include <iostream> int main() { int num = 10; std::cout << "The number is: " << num << std::endl; return 0; } ``` 输出: ``` The number is: 10 ``` 2. 从用户输入读取数据: ```cpp #include <iostream> int main() { int num; std::cout << "Enter a number: "; std::cin >> num; std::cout << "You entered: " << num << std::endl; return 0; } ``` 输入: ``` Enter a number: 20 ``` 输出: ``` You entered: 20 ``` 3. 自定义输出运算符重载: ```cpp #include <iostream> class Point { private: int x, y; public: Point(int x, int y) : x(x), y(y) {} friend std::ostream& operator<<(std::ostream& os, const Point& p) { os << "Point(" << p.x << ", " << p.y << ")"; return os; } }; int main() { Point p(3, 5); std::cout << "The point is: " << p << std::endl; return 0; } ``` 输出: ``` The point is: Point(3, 5) ``` 4. 自定义输入运算符重载: ```cpp #include <iostream> class Point { private: int x, y; public: Point() {} friend std::istream& operator>>(std::istream& is, Point& p) { std::cout << "Enter x and y coordinates: "; is >> p.x >> p.y; return is; } friend std::ostream& operator<<(std::ostream& os, const Point& p) { os << "Point(" << p.x << ", " << p.y << ")"; return os; } }; int main() { Point p; std::cin >> p; std::cout << "The point is: " << p << std::endl; return 0; } ``` 输入: ``` Enter x and y coordinates: 3 5 ``` 输出: ``` The point is: Point(3, 5) ``` 这些是一些常见的`>>`和`<<`运算符的使用示例。在C++,我们可以通过重载这些运算符来自定义输入和输出操作,以适应我们的自定义类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值