Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this lab, we are going to write our own complex number class (Complex), whic

ID: 3566477 • Letter: F

Question

For this lab, we are going to write our own complex number class (Complex), which will overload quite a few operators to make it behave as we would expect a complex number to. It needs to have two private data members (double real and double imaginary). It should have a default constructor, which sets both data members to zero, as well as a constructor that can take both real and imaginary coefficients directly).

Complex(); // creates 0+0i

Complex(2, -1.5); // creates 2-1.5i

You also need to provide standard getters/settings for the two required data members.

You can include any other methods or data members you need/want to support the required behaviors for the class, but it must have the constructors and getters/setters specified above, and it must support all of the operations listed in section

2.1 Overloaded Operators for Complex Class

Complex + Complex

Explanation / Answer

#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex {
private:
   double real;
   double imaginary;

   Complex(){
       real=0;
       imaginary=0;
      
   }
   Complex(double rel,double img){
       real=rel;
       imaginary=img;
   }
   void setReal(double rel){
       real=rel;
   }

   double getReal(){
       return real;
   }

   void setImaginary(double img){
       imaginary=img;
   }
   double getImaginary(){
       return imaginary;
   }
   void displayComplexNumber(){
       cout <<" Real part :"<<real;
       cout <<" Imaginary part : "<<imaginary;
   }
   Complex operator+(Complex obj){
       double tmpReal;
       double tmpImaginary;
       tmpReal=real+obj.getReal();
       tmpImaginary=imaginary+obj.getImaginary();
      
       Complex tmp= Complex(tmpReal,tmpImaginary);
       return tmp;
   }
   Complex operator-(Complex obj){
       double tmpReal;
       double tmpImaginary;
       tmpReal=real-obj.getReal();
       tmpImaginary=imaginary-obj.getImaginary();
      
       Complex tmp= Complex(tmpReal,tmpImaginary);
       return tmp;
   }
   Complex operator+(double rel){
       double tmpReal;
       double tmpImaginary;
       tmpReal=real+rel;
       tmpImaginary=imaginary;
      
       Complex tmp= Complex(tmpReal,tmpImaginary);
       return tmp;
   }
   Complex operator-(double rel){
       double tmpReal;
       double tmpImaginary;
       tmpReal=real-rel;
       tmpImaginary=imaginary;
      
       Complex tmp= Complex(tmpReal,tmpImaginary);
       return tmp;
   }
  
   bool operator==(Complex obj){
       if(real==obj.getReal() && imaginary==obj.getImaginary()){
           return true;
       }
       else{
           return false;
       }
   }
   Complex operator +=(Complex obj){
       double tmpReal;
       double tmpImaginary;
       real=real+obj.getReal();
       imaginary=imaginary+obj.getImaginary();
       return *this;
   }
   Complex operator +=(double rel){
       double tmpReal;
       double tmpImaginary;
       real=real+rel;
       return *this;
   }
   Complex operator- (){
       double tmpReal;
       double tmpImaginary;
       tmpReal=real*(-1);
       tmpImaginary=imaginary*(-1);
       Complex tmp = Complex(tmpReal,tmpImaginary);
       return tmp;
   }

   Complex operator* (Complex obj){
       double tmpReal;
       double tmpImaginary;
       tmpReal=(real*obj.getReal())-(imaginary*obj.getImaginary());
       tmpImaginary=(real*obj.getImaginary())+(imaginary*obj.getReal());
       Complex tmp = Complex(tmpReal,tmpImaginary);
       return tmp;
   }
   Complex operator* (double rel){
       double tmpReal;
       double tmpImaginary;
       tmpReal=real*rel;
       tmpImaginary=imaginary*rel;
       Complex tmp = Complex(tmpReal,tmpImaginary);
       return tmp;
   }
   Complex operator/ (double rel){
       double tmpReal;
       double tmpImaginary;
       tmpReal=real/rel;
       tmpImaginary=imaginary/rel;
       Complex tmp = Complex(tmpReal,tmpImaginary);
       return tmp;
   }
   Complex operator/ (Complex obj){
       double tmp1,tmp2;
       if(imaginary!=0){
       tmp1=(real*obj.getReal()+imaginary*obj.getImaginary());
       tmp1=tmp1/((obj.getReal()*obj.getReal())+(obj.getImaginary()*obj.getImaginary()));
       tmp2=((imaginary*obj.getReal())-(real*obj.getImaginary()));
       tmp2=tmp2/((obj.getReal()*obj.getReal())+(obj.getImaginary()*obj.getImaginary()));
       }
       else //case when b=0 imagainary part of first complex number is zero
       {
          
       tmp1=(real*obj.getReal());
       tmp1=tmp1/((obj.getReal()*obj.getReal())+(obj.getImaginary()*obj.getImaginary()));
       tmp2=imaginary*obj.getReal();
       tmp2=tmp2/((obj.getReal()*obj.getReal())+(obj.getImaginary()*obj.getImaginary()));
       }
       Complex tmp = Complex(tmp1,tmp2);
       return tmp;
   }
   double operator~ (){
       double tmpReal;
       double tmpImaginary;
       tmpReal=real*real;
       tmpImaginary=imaginary*imaginary;
       tmpReal+=tmpImaginary;
       return tmpReal;
   }  
   friend ostream &operator<<( ostream &output, Complex obj){
       output<<obj.getReal();
       return output;
   }
   friend istream &operator<<( istream &input, Complex obj){
       cout <<"enter real part";
       double var;
       cin >>var;
       obj.setReal(var);
       cout <<"enter Imaginary part " ;
       cin >>var;
       obj.setImaginary(var);
       return input;
};
#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote