Answer to Question #222536 in C++ for Jayanth

Question #222536
Define a class named COMPLEX for representing complex numbers that contains necessary data members and member functions. A complex number has the general form a + ib, where a is the real part and b is the imaginary part (i stands for imaginary). Include methods for all the four

basic arithmetic operators.
1
Expert's answer
2021-08-02T07:15:52-0400
#include<iostream>
using namespace std;
  
class Complex {
private:
    int re, im;
public:
    Complex(int r = 0, int i =0)  {re = r;   im = i;}
    
      Complex operator - (Complex const &ob) {
         Complex comp;
         comp.re = re - ob.re;
         comp.im = im - ob.im;
         return comp;
    }
    Complex operator * (Complex const &ob) {
         Complex comp;
         comp.re= re * ob.re;
         comp.im = im * ob.im;
         return comp;
    }
    Complex operator / (Complex const &ob) {
         Complex comp;
         comp.re = re / ob.re;
         comp.im= im / ob.im;
         return comp;
    }
    Complex operator + (Complex const &ob) {
         Complex comp;
         comp.re = re + ob.re;
         comp.im= im + ob.im;
         return comp;
    }
    void print() { cout << re << " + i" << im << endl; }
};
  
int main()
{
    Complex comp1(10, 5), comp2(2, 4);
    Complex comp3 = comp1 + comp2; 
    Complex comp4 = comp3 - comp1;
    Complex comp5 = comp1 * comp2;
    Complex comp6 = comp1 / comp2;
    comp3.print();
    cout<<endl;
    comp4.print();
    cout<<endl;
    comp5.print();
    cout<<endl;
    comp6.print();
}

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