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

This lab is to introduce students to the concept of operator overloading as memb

ID: 3537446 • Letter: T

Question

This lab is to introduce students to the concept of operator overloading as member functions of a class. 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.

Each lab exercise should have a separate section in the lab-report document.

Your lab grade is based upon

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.

STEP 2: Create and Add the Overloaded Operators

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.

Deliverables

Explanation / Answer

#include <iostream>
using namespace std;

class complx
{
      double real,imag;
public:
      complx( double real = 0., double imag = 0.); // constructor
      complx operator+(const complx&) const;       // operator+()
      complx operator-(const complx&) const;       // operator-()
      complx operator*(const complx&) const;       // operator*()
      complx operator/(const complx&) const;       // operator/()
      complx operator=(const complx&) const;       // operator=()
};

// define constructor
complx::complx( double r, double i )
{
      real = r; imag = i;
}

// define overloaded + (plus) operator
complx complx::operator+ (const complx& c) const
{
      complx result;
      result.real = (this->real + c.real);
      result.imag = (this->imag + c.imag);
      return result;
}
// define overloaded - (minus) operator
complx complx::operator+ (const complx& c) const
{
      complx result;
      result.real = (this->real - c.real);
      result.imag = (this->imag - c.imag);
      return result;
}
// define overloaded * (multiplication) operator
complx complx::operator+ (const complx& c) const
{
      complx result;
      result.real = (this->real * c.real)-(this->imag * c.imag);
      result.imag = (this->real * c.imag)+(this->imag * c.real);
      return result;
}
// define overloaded / (divide) operator
complx complx::operator+ (const complx& c) const
{
      complx result;
      float com,rel1,rel2 ;
if(c.real!=0 && c.imag !=0)   {
      rel1 = (this->real * c.real)-(this->imag * c.imag);
      rel2 = (this->real * c.imag)+(this->imag * c.real);
      com = c.real*c.real + c.imag*c.imag;
      result.real = rel1/com ;
      result.imag = rel2/com ;
      return result;
}
return 0 ;
}
// define overloaded + (equalto) operator
complx complx::operator= (const complx& c) const
{
      complx result;
      result.real = c.real;
      result.imag = c.imag;
      return result;
}

int main()
{
      complx x(4,4);
      complx y(6,6);
      complx z1 = x + y; // calls complx::operator+()
      complx z2 = x - y; // calls complx::operator-()
      complx z3 = x * y; // calls complx::operator*()
      complx z4 = x / y; // calls complx::operator/()
        cout<< Addition of complex number << z1 <<endl ;
    cout<< subtration of complex number << z2 <<endl ;
    cout<< multiplication of complex number << z3 <<endl ;
    cout<< divide of complex number << z4 <<endl ;

}

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