|
-
Jan 5th, 2003, 09:35 AM
#1
Thread Starter
Addicted Member
iostream
#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++?
-
Jan 5th, 2003, 12:02 PM
#2
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;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 6th, 2003, 02:29 AM
#3
Thread Starter
Addicted Member
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
-
Jan 6th, 2003, 08:56 AM
#4
Weird. Have you stepped through it with a debugger?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 6th, 2003, 08:39 PM
#5
Thread Starter
Addicted Member
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++
-
Jan 6th, 2003, 09:37 PM
#6
Thread Starter
Addicted Member
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.
-
Jan 7th, 2003, 07:30 AM
#7
Dereference it:
complex *pc = new complex(1.2, 3.4);
cout << *pc << endl;
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|