Answer to Question #183052 in C++ for Rocky bhai

Question #183052

Develop a C++ program to Subtract two complex number by overloading - operator using Friend function.


1
Expert's answer
2021-04-19T03:54:21-0400
#include <iostream>
#include <cmath>


using namespace std;


class Complex
{
private:


  double _re, _im;


public:


  double GetReal()const
  {
      return _re;
  }


  double GetImagine()const
  {
      return _im;
  }


  Complex ()
  {
      _re = 0;
      _im = 0;
  };


  Complex (double r, double i)
  {
    _re = r;
    _im = i;
  }




  friend Complex operator-(Complex c1, Complex c2);




};


  Complex operator-(Complex c1, Complex c2)
{
    Complex c3;


    c3._re = c1._re - c2._re;
    c3._im = c1._im - c2._im;


    return c3;
}




int
main ()
{
  Complex a (5, 2);
  Complex b (3, -3);
  Complex c = a - b;
  cout << "Your complex number: " << c.GetReal() << " + " << c.GetImagine() << "i";


  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS