thecplusplusguy

istream example

Jul 2nd, 2011
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class thing{
  5.     double something;
  6.     public:
  7.     thing(double d) : something(d) {}; //constructor with member initialization list
  8.     friend istream& operator>>(istream&,thing&); //the istream operator overloading
  9. };
  10.  
  11. istream& operator>>(istream& in,thing& t) //the definition of the operator overloading (global)
  12. {
  13.     cout << "Write a number: "; //first we just write something to the user
  14.     //you can do other thing in here as well (similarly to ostream), for example check the correctness of the entered data...
  15.     in >> t.something;  //then read it from in to something
  16.     return in; //finally return with the istream
  17. }
  18.  
  19.  
  20. int main ()
  21. {
  22.     thing t(5.5);
  23.     cin >> t;
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment