Question: Implement a class for arithmetic using imaginary numbers ( a + i b ) . The class has the following constructors and overloaded operators: *

Implement a class for arithmetic using imaginary numbers (a + i b). The class has the following constructors and overloaded operators:
*A constructor with no arguments sets the real and imaginary parts of the object to values 0.0.
*A constructor with two arguments a and b sets the real part to parameter a, and the imaginary part to parameter b.
*Operator + adds to imaginary numbers.
*Operator - subtracts two imaginary numbers.
*Operator * multiplies two imaginary numbers.
*Operator < displays an imaginary number.
*Operator < reads an imaginary number.
*Operator = assigns the right-side object to the left side object, e.g., a = b.
*Operator = returns true if two imaginary numbers are equal and falseotherwise.
*Operator!= returns true if two imaginary numbers are different and false otherwise.
The main program should demonstrate the use of all the above constructors and overloaded operators.
And fix the error of the code below.
#include
#include
#include
using namespace std;
class Imaginary {
public:
Imaginary ();
Imaginary (float a, float b);
Imaginary& operator+(Imaginary &a);
operator=(Imaginary& v);
friend ostream& operator<<(ostream& out, Imaginary& a);
private:
float real;
float imag;
};
Imaginary :: Imaginary (){
real =0.0; imag =0.0;
}
Imaginary :: Imaginary (float a, float b){
real = a; imag = b;
}
Imaginary& Imaginary :: operator+(Imaginary& a){
Imaginary v;
v.real = real + a.imag;
v.imag = imag + a.imag;
return v;
}
Imaginary :: operator=(Imaginary& v){
real = v.real;
imag = v.imag;
}
ostream& operator<<(ostream& out, Imaginary& a){
out << a.real <<"+ i "<< a.imag << endl;
return out;
}
int main(){
class Imaginary in1(3.5,4.5), in2(1.0,1.0), in3;
in3= in1+ in2;
cout << in3;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!