Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

overload stream extract cpp

struct coord {
    int x, y;

    /// take 2 inputs and put into coord, using overloaded >> operator
	friend istream& operator>>(istream& input, coord &c) {
		input >> c.x >> c.y;
		return input;
    }
	/// extract coord in point notation
	friend ostream& operator<<(ostream& output, const coord &c) {
      output << "(" << c.x << "," << c.y << ")"; // (x,y)
      return output;
   }
};

//usage:
coord pnt;
cin >> pnt; //cin contains: 1 2
cout << pnt; // (1,2)
Source by www.tutorialspoint.com #
 
PREVIOUS NEXT
Tagged: #overload #stream #extract #cpp
ADD COMMENT
Topic
Name
1+3 =