Total members 11895 |It is currently Sat Dec 21, 2024 4:02 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





here we Overload stream operator
cpp 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;
}




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 95
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : stream operator
 stream get filters     -  
 stream filter to append     -  
 Read stream from URL using Java     -  
 BufferedReader.ready() true at end of stream ?     -  
 How to stream a live video from a dlink     -  
 Using the ? Operator     -  
 What is the % operator     -  
 operator int()     -  
 What is Operator Overloading? !!!     -  
 trinary operator     -  



Topic Tags

C++ OOP






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com