Here is the error I am Facing In Results I am trying to make a program that Takes two complex Numbers from User and then in Output it shows Addition, Subtraction and Multiplication Of these Numbers I have Provided mY code Below But It Does not Show Correct Output I have Tried very much But it's not working a little Help would be appreciated
#include<iostream>
using namespace std;
class complexnumber
{
int real, image;
public:
{
real = real;
image = image;
}
complexnumber operator +(complexnumber c2)
{
complexnumber temp;
temp.real = real + c2.real;
temp.image = image + c2.image;
return temp;
}
complexnumber operator-(complexnumber c2)
{
complexnumber temp;
temp.real = real - c2.real;
temp.image = image - c2.image;
return temp;
}
complexnumber operator*(complexnumber c2)
{
complexnumber temp;
temp.real = real * c2.real + (image *c2.image)*(-1);
temp.image = image * c2.real+c2.image*real;
return temp;
}
void display(complexnumber c)
{
if (c.image < 0)
{
cout << real << image << "i";
}
else
{
cout << real << "+" << image << "i";
}
}
};
int main()
{
int real1, real2, image1, image2;
cout << "enter real value 1:";
cin >> real1;
cout << endl;
cout << "enter real value 2:";
cin >> real2;
cout << endl;
cout << "enter imaginary value 1:";
cin >> image1;
cout << endl;
cout << "enter imaginary value 2:";
cin >> image2;
cout << endl;
complexnumber c1, c2, c3, c4, c5;
c1.set(real1, image1);
c2.set(real2, image2);
c3 = c1 + c2;
c4 = c1 - c2;
c5=c1*c2;
cout << "addition of two complex number ";
c3.display(c3);
cout << endl;
cout << "subtraction of two complex number ";
c4.display(c4);
cout << endl;
cout << "multiplication of two complex number ";
c5.display(c5);
cout << endl;
return 0;
}
public:
{
real = real;
image = image;
}
I'm assuming this is your set
function that you are invoking in main
public:
void set(int real, int image){
real = real;
image = image;
}
Here this is not setting the member variables real
and image
to the parameter values from fucntion set
. This is just assigning the passed variable back to itself. The compiler will first look for a local variable or constructor argument called "real", and only if it doesn't find one will it start looking for a class member called "real". The result is that the above statment "real=real" will assign the value stored in argument "real" to argument "real" rendering it a useless statement.
public:
void set(int real, int image){
this->real = real;
this->image = image;
}
This will get the desired output.
This code helps. :)
int real, image; public: void set(int real, int image) { this->real = real; this->image = image; } complexnumber operator +(complexnumber c2) { complexnumber temp; temp.real = real + c2.real; temp.image = image + c2.image; return temp; } complexnumber operator-(complexnumber c2) { complexnumber temp; temp.real = real - c2.real; temp.image = image - c2.image; return temp; } complexnumber operator*(complexnumber c2) { complexnumber temp; temp.real = real * c2.real + (image *c2.image)*(-1); temp.image = image * c2.real+c2.image*real; return temp; } void display() { if (this->image < 0) { cout << this->real << this->image << "i"; } else { cout << this->real << "+" << this->image << "i"; } } }; int main() { int real1, real2, image1, image2; cout << "enter real value 1:"; cin >> real1; cout << endl; cout << "enter real value 2:"; cin >> real2; cout << endl; cout << "enter imaginary value 1:"; cin >> image1; cout << endl; cout << "enter imaginary value 2:"; cin >> image2; cout << endl; complexnumber c1, c2, c3, c4, c5; c1.set(real1, image1); c2.set(real2, image2); c3 = c1 + c2;//(1+3i)+(2+4i) c4 = c1 - c2;//(1+3i)-(2+4i) c5= c1 * c2; //(1+3i)(2+4i) = (1)(2)+(1)(4i)+(3i)(2)+(3i)(4i) // = (2) +(4i)+(6i)(12i^2) // = 2+10i+12i^2 //(i^2 == -1) = 2 + 10i-12 = -10+10i cout << "addition of two complex number "; c3.display();//Dont need to pass object. Its already has c3 object cout << endl; cout << "subtraction of two complex number "; c4.display(); cout << endl; cout << "multiplication of two complex number "; c5.display(); cout << endl; return 0; }
Let me know for any clarifications. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.