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

This will be done in the context of creating a class that will perform four basi

ID: 3638243 • Letter: T

Question

This will be done in the context of creating a class that will perform four basic mathematical operations on complex numbers.

The C++ compiler has defined operators for all the arithmetic and assignment operations for its native data types, such as integer, float, double, and so forth. However, for user-defined data types, such as classes and structures, these operations are undefined.

C++ allows the programmer to create new definitions for these operators so that they can operate specifically on these user-defined data types. Which set of operators is actually called and implemented is decided during the compilation, and it is based on the data types of the operands involved in the operation.

The ability to define a new set of data-type, dependent operations on existing operators, or functions, is called operator overloading.

STEP 1: Create a New Multifile Project

Create a new multifile project, and create a class to model complex-number, mathematical operations using overloaded operators and add main() test functions to test the operations.

Create a class for complex numbers that will provide the means to store both the real and the imaginary values of a complex number.
Design and add overloaded operators to perform correct and complex mathematical operations on objects of the complex-number class.

STEP 2: Create and Add the Overloaded Operators


Overload the addition operator (+) to perform complex mathmatical addition on two objects of the complex-number class.
Overload the subtraction operator (-) to perform complex mathmatical subtraction on two objects of the complex-number class.
Overload the multiplication operator (*) to perform complex mathmatical multiplication on two objects of the complex-number class.
Overload the division operator (/) to perform complex mathmatical division on two objects of the complex-number class.
Overload the assignment operator (=) to assign all the data values of an object in the complex-number class to another object in the complex-number class.


STEP 3: Test the Overloaded Operators


Create at least three objects of the complex-number class in function main() and use them to perform the following operation tests.

1. Demonstrate the correct operation of the complex addition overloaded operator.
2. Demonstrate the correct operation of the complex subtraction overloaded
operator.
3. Demonstrate the correct operation of the complex division overloaded operator.
4. Demonstrate the correct operation of the complex multiplication overloaded operator.




Explanation / Answer

#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;


class complex
{
     private:
                  float real;               // Real Part
            float imag;      // Imaginary Part


   public:
          complex(float,float);
          complex(complex&);
          complex operator +(complex);
          complex operator -(complex);
          complex operator *(complex);
          complex operator /(complex);
          void setdata(float,float);
          void getdata();
          float getreal();
          float getimaginary();
          friend ostream& operator <<(ostream &s,complex &c);
};
//                                        CONSTRUCTOR
                  complex::complex(float r=0.0f,float im=0.0f)
            {
                 real=r;
               imag=im;
            }

//                                 COPY CONSTRUCTOR
            complex::complex(complex &c)
            {
                 this->real=c.real;
               this->imag=c.imag;
            }


            complex complex::operator +(complex c)
            {
                 complex tmp;
               tmp.real=this->real+c.real;
               tmp.imag=this->imag+c.imag;
               return tmp;
            }

            complex complex::operator -(complex c)
            {
                 complex tmp;
               tmp.real=this->real - c.real;
               tmp.imag=this->imag - c.imag;
               return tmp;
            }

                    complex complex::operator *(complex c)
            {
                 complex tmp;
               tmp.real=(real*c.real)-(imag*c.imag);
               tmp.imag=(real*c.imag)+(imag*c.real);
               return tmp;
            }

            complex complex::operator /(complex c)
            {
                 float div=(c.real*c.real) + (c.imag*c.imag);
               complex tmp;
               tmp.real=(real*c.real)+(imag*c.imag);
               tmp.real/=div;
               tmp.imag=(imag*c.real)-(real*c.imag);
               tmp.imag/=div;
               return tmp;
            }


            void complex::setdata(float r,float i)
            {
                 real=r;
               imag=i;
            }

            void complex::getdata()
            {
                 cout<<"Enter Real:";
               cin>>this->real;
               cout<<"Enter Imaginary:";
               cin>>this->imag;

            }

            float complex::getreal()
            {
                 return real;
            }

            float complex::getimaginary()
            {
                 return imag;
            }

              ostream& operator <<(ostream &s,complex &c)
            {
                 s<<"Real Part = "<<c.real<<endl
                <<"Imaginary Part = "<<c.imag<<endl;
               s<<"z = "<<c.real<<setiosflags(ios::showpos)
                <<c.imag<<"i"<<endl<<resetiosflags(ios::showpos);
                return s;
            }



int main()
{
     complex a(10.0f,-2.f); // Calls Constructor
   cout<<a<<endl;               // Calls the overloaded << operator
   complex b(-2);         // Calls Constructor
   complex c=b;                    // Calls Copy Constructor
   c=a;                                   // calls overloaded = operator
   b.getdata();                    // Calls Getdata()
   c.getdata();

   complex d;
   d=a+b;   // Calls overloaded + LINE 176
   cout<<d<<endl;
   d=a-b;     // Calls overloaded - LINE 178
   cout<<d<<endl;
   d=a*b;        // calls overloaded * LINE 180
   cout<<d<<endl;
   d=a/b;        // calls overloaded /    LINE 182
   cout<<d<<endl;

   return 0;
}

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