DEV Community

Cover image for I/O Stream or Shift Operators Overloading | C++
Yash Desai
Yash Desai

Posted on

2 1

I/O Stream or Shift Operators Overloading | C++

The Data class

#include <iostream>
using namespace std;

class Data
{
private:
    int n, m;
public:
    Data() { n = 0; m = 0; }
    Data(int n, int m) { this->n = n; this->m = m; }

    //stuff for ostream operator << ...
    //stuff for istream operator >> ...
};
Enter fullscreen mode Exit fullscreen mode

Inside "Data" class...

    friend ostream &operator<<(ostream &myout, Data &data)
    {
        myout << "n: " << data.n << " m: " << data.m << endl;
        return myout;
    }
    friend istream &operator>>(istream &myin, Data &data)
    {
        myin >> data.n;
        myin >> data.m;

        return myin;
    }
Enter fullscreen mode Exit fullscreen mode

Now the main()

int main()
{
    Data data(5, 7);
    Data data1;

    cout << data;

    cin >> data1;

    cout << data1;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output

Alt Text

Also available on YouTube

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs