Mar 21, 2015 at 6:52pm UTC
I have created A class Program With C++
But I don't Know Why This outputs strange numbers like -858993460
And This Is The Program :
#include<iostream>
using namespace std;
class complex
{
private:
int r;
int im;
public:
void setc(int a, int b)
{
a=r;
b=im;
}
complex (){};
complex (int a ,int b){a=r; b=im;};
complex operator++()
{
r++;
im++;
return *this;
}
complex operator=(complex &x)
{
r=x.r;
im=x.im;
return *this;
}
complex operator+=(complex &x)
{
r+=x.r;
im+=x.im;
return *this;
}
friend complex operator +(complex x,complex y)
{
complex z;
z.r=x.r +y.r;
z.im= x.im + y.im;
return z;
}
friend complex operator -(complex x,complex y)
{
complex z;
z.r= x.r - y.r;
z.im= x.im - y.im;
return z;
}
friend complex operator *(complex x,complex y)
{
complex z;
z.r= x.r * y.r;
z.im= x.im * y.im;
return z;
}
friend istream& operator>>(istream& in,complex x)
{
return in>>x.r>>x.im;
}
friend ostream& operator<<(ostream& out,complex x)
{
return out<<x.r<<" + " <<x.im;
}
};
int main(){
complex c1,c2;
cout<< "enter first complex number ";
cin>>c1;
cout<< "enter second complex number ";
cin>>c2;
cout<<c1<<endl<<c2<<endl;
cout<<endl;
return 0;
}
Please Help Me
Mar 21, 2015 at 7:03pm UTC
1 2 3 4 5
void setc(int a, int b)
{
a=r;
b=im;
}
The variable on the left is what gets the new value.
You're currently assigning r/im to the local variables a/b, which is junk.
You want to switch your assignments.
Last edited on Mar 21, 2015 at 7:03pm UTC