Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

XML Data Management Native XML And XML Enabled Database Systems

Authors: Akmal Chaudhri, Awais Rashid, Roberto Zicari, John Fuller

1st Edition

0201844524, 978-0201844528

More Books

Students also viewed these Databases questions

Question

state what is meant by the term performance management

Answered: 1 week ago