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

C++ (class,constructors ,overloading operator) In this assignment you are requir

ID: 3905218 • Letter: C

Question

C++ (class,constructors ,overloading operator)

In this assignment you are required to implement and test the class Complex for performing arithmetic with complex numbers You shall use all the concepts you learned so far, more precisely how to use constructors (e.g when an object is created) and how to overload basic operators such as +, *, and the output and input operators Recall that complex numbers have the form reaiPart imagina r vPart * i where is equal to the square root of (-1) We next give the basic class Complex that you are expected to extend and implement. #ifndef COMPLEXH #define COMPLEX H - class Complex ublic: Complex (doble 0.0, double 0.0 );// default constructor double getReal ) const, double getlmaginary () const void setReal (double); void setlmaginary (double) // returns real part // returns imaginary part // sets real part // sets imaginary part rivate: double real double imaginary l: // end class Complex #endif

Explanation / Answer

#include<iostream>

class Complex
{
       private:
       double real;
       double imaginary;
       public:
   Complex(double=0.0,double=0.0); //default constructor
   double getReal()const;   //returns real part
   double getImaginary()const; //returns imaginary part
   void setReal(double);   //sets real part
   void setImaginary(double); //sets imaginary part

   bool operator==(const Complex & c)
   {
       if ((real==c.real)&&(imaginary==c.imaginary))
          return 1;
          else return 0;
   }
  
   Complex&operator=(const Complex& c)
   {
      real=c.real;
      imaginary=c.imaginary;
      return *this;  
   }
  
   Complex & operator+=(const Complex & c)
   {
       real=real+c.real;
       imaginary=imaginary+c.imaginary;
       return * this;
   }
  
   Complex & operator-=(const Complex & c)
   {
       real=real-c.real;
       imaginary=imaginary-c.imaginary;
       return * this;
   }
   Complex & operator*=(const Complex & c)
   {
       real=(real*c.real)-(imaginary*c.imaginary);
       imaginary=(imaginary*c.real)+(real*c.imaginary);  
       return * this;
   }
  
friend istream &operator >>(istream&din,Complex & C);
friend   ostream & operator <<(ostream & dout,const Complex C);  
}; //end of class complex


Complex operator +( const Complex &c1,const Complex & c2)
{
Complex c3;
c3.real=c1.real+c2.real;
c3.imaginary=c1.imaginary+c2.imaginary;
return c3;
}
Complex operator -(const Complex &c1,const Complex & c2)
{
Complex c3;
c3.real=c1.real-c2.real;
c3.imaginary=c1.imaginary-c2.imaginary;  
return c3;
}
Complex operator *(const Complex &c1,const Complex & c2)
{
Complex c3;
c3.real=(c1.real*c2.real)-(c1.imaginary*c2.imaginary);
c3.imaginary=(c1.imaginary*c2.real)+(c1.real*c2.imaginary);
return c3;
}

istream &operator >>(istream&din,Complex & C)
{
    din>>real>>imaginary;
    return din;
}

ostream & operator <<(ostream & dout,const Complex C)
{
    dout<<C.real<<"+"<<C.imaginary;
    return dout;
   
}


int main()
{
   
   
   
    Complex C1,C2,C3,C4,C5,C6,C7,C8;
    C1.setReal(3.5);
    cout<<"C1= ";
C1.getReal();
C1.getImaginary();
    C1.setImaginary(4.5);
    C2.setReal(2.5);
    C2.setImaginary(1.5);
    cout<<"C2= ";
C2.getReal();
C2.getImaginary();
    C6.setReal(3.5);
    C6.setImaginary(4.5);
    cout<<C6;      //invokes operator <<
    C3=C1+C2;
    cout<<C3;    //invokes operator <<
    C4=C1-C2;
    cout<<C4;   //invokes operator <<
    C5=C1*C2;
    cout<<C5;   //invokes operator <<
    if (C1==C6)
    cout<<"The complex numbers are equal";
    else
    cout<<"They are not equal";
    C2+=C1;    //invokes operator +=
    cout <<C2; //invokes operator <<
    C2-=C1;    //invokes operator -=
    cout<<C2;   //invokes operator <<
    C2*=C1;     //invokes operator *=
    cout<<C2;
    C8=C1; //Assignment operator
    cout<<C8; //invokes operator <<
   
   
    return 0;
}