4 Replies - 238 Views - Last Post: 28 February 2018 - 07:09 AM Rate Topic: -----

#1 Bihar Shooter   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 13-February 18

Question on overloading stream operators.

Posted 26 February 2018 - 08:55 AM

#include <iostream>

using namespace std;

class data
{
    int x,y;
public:
    data ()
    {
        x=0;
        y=0;
    }

    void display()
    {
        cout<<x<<","<<y<<endl;
    }
//can't understand !!/////////////////
    friend istream & operator >>(istream & in , data & obj)
    {
        cout<<"Enter for X -"<<endl;
        in >> obj.x;
        cout<<"Enter for Y -"<<endl;
        in >> obj.y;

        return in;
    }
//can't understand !!//////////////////
};


Q1 pls explain how that ^ ^ ^ friend function works ?
Q2 what is the purpose of returning by reference ?
Q3 does ' istream ' ONLY mean 'cin' ?
Q4 is it possible to have this function without friend keyword ?(if yes , how ?)

thanks :gun_bandana:/>

edit:
i know how friend functions work
i know how call by reference works

i am asking what is purpose of returning by reference in that specific function
and how that func. works
:gun_bandana:/>

Is This A Good Question/Topic? 0
  • +

Replies To: Question on overloading stream operators.

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7183
  • View blog
  • Posts: 14,971
  • Joined: 16-October 07

Re: Question on overloading stream operators.

Posted 26 February 2018 - 09:21 AM

View PostBihar Shooter, on 26 February 2018 - 10:55 AM, said:

i am asking what is purpose of returning by reference in that specific function
and how that func. works


Chaining. That is, when you see cout << foo << " says hi" whats happening is that cout << foo is getting all wrapped into something like operator >>(cout, foo). Now, if that doesn't return cout, the next statement doesn't work. e.g. op(op(cout,foo),"says hi"). Neat, huh?

Since friend bugs me, here's some quick example code without it.
#include <iostream>

class Point {
private:
    int x, y;
public:
    Point();
    Point(int x, int y);
    // no friends, but we do have print and read
    void print(std::ostream &) const;
    void read(std::istream &);
};
std::istream &operator >>(std::istream &, Point &);
std::ostream &operator <<(std::ostream &, const Point &);

int main() {
    std::cout << Point() << ' ' << Point(2, 3) << std::endl;
    Point pt(3, 4);
    std::cout << pt << " enter new value: x y" << std::endl;
    std::cin >> pt;
    std::cout << "new value:" << pt << std::endl;
    system("pause");
    return 0;
}

Point::Point(int px, int py) : x(px), y(py) {}
Point::Point() : Point(0,0) {}
void Point::print(std::ostream &os) const { os << "Point(" << x << ',' << y << ')'; };
void Point::read(std::istream &is) { is >> this->x >> this->y; }

// now these are pretty easy
std::istream &operator >>(std::istream &is, Point &p) { p.read(is); return is; }
std::ostream &operator <<(std::ostream &os, const Point &p) { p.print(os); return os; }



Hope this helps.

This post has been edited by baavgai: 26 February 2018 - 09:47 AM
Reason for edit:: dyslexia is fun

Was This Post Helpful? 1
  • +
  • -

#3 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 832
  • View blog
  • Posts: 3,353
  • Joined: 13-June 14

Re: Question on overloading stream operators.

Posted 26 February 2018 - 01:26 PM

View PostBihar Shooter, on 26 February 2018 - 03:55 PM, said:

Q3 does ' istream ' ONLY mean 'cin' ?


Of course not. istream is a type (in the same way that string or int are types), so you can pass anything that is an istream (i.e. an instance of istream or its subclasses, like an ifstream if you wanted to read from a file). I've highlighted the words is a(n) here on purpose, because this is about inheritance and polymorphism.
Was This Post Helpful? 1
  • +
  • -

#4 Bihar Shooter   User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 39
  • Joined: 13-February 18

Re: Question on overloading stream operators.

Posted 28 February 2018 - 03:20 AM

I understood thanks!
The program works fine if i don't return istream & and have void instead of it.
I want to know what is the advantage of returning 'istream &'
(Refer to my posted code)
:gun_bandana:
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6221
  • View blog
  • Posts: 21,472
  • Joined: 05-May 12

Re: Question on overloading stream operators.

Posted 28 February 2018 - 07:09 AM

If you understood post #2, then you wouldn't be asking why there is an advantage to returning the stream reference.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1