PDA

Click to See Complete Forum and Search --> : iostream


purusingh
Jan 5th, 2003, 08:35 AM
#include <iostream.h>

class complex
{
float real,imag;
public:
complex();
complex(float a,float b);
friend ostream& operator<< ( ostream& os, complex& dt );
friend istream& operator>> ( istream& is, complex& dt );
};


#include "complex.h"

complex::complex()
{
real=imag=0.0;
}
complex::complex(float a,float b)
{
real=a;
imag=b;
}
ostream& operator<< ( ostream& s, complex& dt )
{
s << dt.real << dt.imag;
return s;
}
istream& operator>> ( istream& s, complex& dt )
{
s >> dt.real >> dt.imag;
return s;
}






#include "complex.h"
#include <fstream.h>
#include <conio.h>

void main()
{

char c;

cout <<"Press s for save "<<endl;
cin >>c;

if (c=='S' || c=='s')
{
complex a=complex(1.2,3.4);
ofstream os("c:\\abc.txt");
os<<a;
cout << a;
}
else
{

complex a;
ifstream is("c:\\abc.txt");
is >> a;
cout << a;
}

while (!kbhit());

}


why this program different result in bc++ and vc++?

CornedBee
Jan 5th, 2003, 11:02 AM
What different result?

BTW:
#include <iostream>
#include <fstream>
using namespace std;

And the C++ standard library already defines a complex class:
#include <complex>
using namespace std;

complex<double> c;

purusingh
Jan 6th, 2003, 01:29 AM
bc++ gives the result 1.230.4 and cv++ gives no result. And there is result in bc++ that it should give 1.23.4

CornedBee
Jan 6th, 2003, 07:56 AM
Weird. Have you stepped through it with a debugger?

purusingh
Jan 6th, 2003, 07:39 PM
It is working with bc++ fine when i change?

ostream& operator<< ( ostream& s, complex& dt )
{
s << dt.real << dt.imag;
return s;
}

into

ostream& operator<< ( ostream& s, complex& dt )
{
s << dt.real << ' ' << dt.imag;
return s;
}

but it is not working with vc++

purusingh
Jan 6th, 2003, 08:37 PM
In the above example .What to do if we use pointer.

complex *a=new complex(1.2,3.4);
it gives the address or somethin like that.

CornedBee
Jan 7th, 2003, 06:30 AM
Dereference it:

complex *pc = new complex(1.2, 3.4);
cout << *pc << endl;