Question

You have to implement the other 3 operators, '-', '*', and '/', which are defined as...

You have to implement the other 3 operators, '-', '*', and '/', which are defined as follows. Suppose

Z1 = x1 + y1i
Z2 = x2 + y2i

are complex numbers, then

Z1 - Z2 = (x1 - x2) + (y1 - y2)i

Z1 * Z2 = (x1 x2 - y1 y2) + (x1 y2 + x2 y1)i

Z1 / Z2 = (x1 x2 + y1 y2) / D + (x2 y1 - x1 y2)/D i
where

  • D = x22 + y22
 
 
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for your problem.

#include <iostream>

#include <cmath>

using namespace std;

class Complex

{

private:

    double real, imag;

public:

    Complex(double r = 0, double i = 0)

    {

        real = r;

        imag = i;

    }

    // This is automatically called when '+' is used with

    // between two Complex objects

    Complex operator+(Complex const &obj)

    {

        Complex res;

        res.real = real + obj.real;

        res.imag = imag + obj.imag;

        return res;

    }

    // This is automatically called when '-' is used with

    // between two Complex objects

    Complex operator-(Complex const &obj)

    {

        Complex res;

        res.real = real - obj.real;

        res.imag = imag - obj.imag;

        return res;

    }

    // This is automatically called when '*' is used with

    // between two Complex objects

    Complex operator*(Complex const &obj)

    {

        Complex res;

        res.real = real * obj.real - imag * obj.imag;

        res.imag = real * obj.imag + obj.real * imag;

        return res;

    }

    // This is automatically called when '/' is used with

    // between two Complex objects

    Complex operator/(Complex const &obj)

    {

        Complex res;

        double D = pow(obj.real, 2) + pow(obj.imag, 2);

        res.real = (real * obj.real + imag * obj.imag) / D;

        res.imag = (obj.real * imag - real * obj.imag) / D;

        return res;

    }

    void print()

    {

        cout << real << " + i" << imag << endl;

    }

};

int main()

{

    Complex c1(10, 5), c2(2, 4);

    Complex c3 = c1 + c2; // An example call to "operator+"

    c3.print();

    c3 = c1 - c2;   // An example call to "operator-"

    c3.print();

    c3 = c1 * c2;   // An example call to "operator*"

    c3.print();

    c3 = c1 / c2;   // An example call to "operator/"

    c3.print();

}

Here is the screenshot of the code if the indentation is not clear.

Here is the output of the code.


Hope this helps.

Please rate the answer if you like it.

Do leave a comment.Any suggestion/query is much appreciated.

Add a comment
Know the answer?
Add Answer to:
You have to implement the other 3 operators, '-', '*', and '/', which are defined as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT