Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 2:07 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: stream operator
PostPosted: Thu Nov 13, 2008 8:14 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

here we Overload stream operator
Code:
#include <iostream>
#include <fstream>
using namespace std;

class MyClass {
  int x, y;
public:
  MyClass(int i, int j) {
     x = i;
     y = j;
  }
  friend ostream &operator<<(ostream &stream, MyClass ob);
  friend istream &operator>>(istream &stream, MyClass &ob);
};

ostream &operator<<(ostream &stream, MyClass ob)
{
  stream << ob.x << ' ' << ob.y << '\n';

  return stream;
}

istream &operator>>(istream &stream, MyClass &ob)
{
  stream >> ob.x >> ob.y;

  return stream;
}

int main()
{
  MyClass object1(1, 2), object2(3, 4);
  ofstream out("test");

  if(!out) {
    cout << "Cannot open output file.\n";
    return 1;
  }

  out << object1 << object2;

  out.close();

  ifstream in("test");

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  MyClass object3(0, 0), object4(0, 0);
  in >> object3 >> object4;

  cout << object3 << object4;

  in.close();

  return 0;
}


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 working of bitwise ~ operator  Java  Anonymous  1
 unary operator overloading  C++ examples  mileloader  0
 Sizeof Operator  C++ examples  mileloader  0
 operator int()  C-C++  AskBot  1
 What is the % operator  Java  msi_333  1

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team