C++ CLASS FOR DEFINING COMPLEX NUMBERS (READ BELOW)
Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √ −1. You should represent a complex number here as two values of type double. You should name the variables real and imaginary. You can call the class Complex. You should include a constructor with two parameters of type double that can be used to set the member variables of an object to any values. You can also include a default constructor that initializes the object to 0 (that is to 0+ 0*i). Now, you should overload all of the following operators so that they correctly apply to the type Complex: ==, +, -, >>, and <<. You should write a test program to test your class. Hints: To add or subtract two complex numbers, you should add or subtract two member variables of type double. The product of two complex numbers is given by the following formula: (a + b ∗ i) ∗ (c + d ∗ i) = (a ∗ c − b ∗ d) + (a ∗ d + b ∗ c) ∗ i Sample Input and Output: complex number x = (3, -4) real part: 3 imaginary part: -4 complex number y = (1, -1) real part: 1 imaginary part: -1 z = x + y = (4, -5) z = x * y = (-1, -7) z = x - y = (2, -3)
#include<iostream>
#include<cstring>
#include<cmath>
#include <sstream>
using namespace std;
class Complex
{
double real;
double imaginary;
public:
// default constructor
Complex()
{
real = 1;
imaginary = 0;
}
// overloaded constructor
Complex(double real)
{
this->real = real;
this->imaginary = 0;
}
// overloaded constructor
Complex(double real, double imaginary)
{
this->real = real;
this->imaginary = imaginary;
}
// copy constructor
Complex(const Complex& ob)
{
this->real = ob.real;//ob.getReal();
this->imaginary = ob.imaginary;//ob.getImaginary();
}
// destructor
~Complex()
{
}
// getter methods
double getReal()
{
return this->real;
}
double getImaginary()
{
return this->imaginary;
}
// setter methods
void setReal(double real)
{
this->real = real;
}
void setImaginary(double imaginary)
{
this->imaginary = imaginary;
}
// returns the complex modulus (or complex norm) of the object
double Modulus()
{
return sqrt(real * real + imaginary * imaginary);
}
Complex Complement()
{
// create a new Complex object
Complex ans;
ans.setReal(this->getReal());
ans.setImaginary(-this->getImaginary());
return ans;
}
Complex operator+(Complex ob)
{
// create a new Complex object
Complex ans;
// compute the real part of the resulting complex number
ans.setReal(this->getReal() + ob.getReal());
// compute the imaginary part of the resulting complex number
ans.setImaginary(this->getImaginary() + ob.getImaginary());
return ans;
}
Complex operator-(Complex ob)
{
// create a new Complex object
Complex ans;
// compute the real part of the resulting complex number
ans.setReal(this->getReal() - ob.getReal());
// compute the imaginary part of the resulting complex number
ans.setImaginary(this->getImaginary() - ob.getImaginary());
return ans;
}
Complex operator*(Complex ob)
{
// create a new Complex object
Complex ans;
// compute the real part of the resulting complex number
ans.setReal(this->getReal() * ob.getReal() - this->getImaginary() * ob.getImaginary());
// compute the imaginary part of the resulting complex number
ans.setImaginary(this->getReal() * ob.getImaginary() + this->getImaginary() * ob.getReal());
return ans;
}
Complex operator/(Complex ob)
{
// get the modulous of ob
int mod = ob.Modulus();
if(mod == 0)
throw "Error: Cannot divide by zero.";
// create a new Complex object which is complement of ob
Complex comp = ob.Complement();
// get a new Complex number which is the product of the current
// object and the complement of ob
Complex temp = (*this) * comp;
// create a new Complex object which is the required ans
Complex ans;
// compute the real part of the resulting complex number
ans.setReal(temp.getReal() / mod);
// compute the imaginary part of the resulting complex number
ans.setImaginary(temp.getImaginary() / mod);
return ans;
}
string toString()
{
stringstream x;
x<<this->getReal();
string ans = x.str();
if(this->getImaginary() > 0)
ans += " + ";
else
ans += " - ";
stringstream y;
y<<abs(this->getImaginary());
ans += y.str() + "i";
return ans;
}
// overload ==
bool operator==(Complex& ob)
{
return this->getReal() == ob.getReal() && this->getImaginary() == ob.getImaginary();
}
// overload !=
bool operator!=(Complex& ob)
{
return !(*this == ob);
}
// overload <
bool operator<(Complex& ob)
{
return this->Modulus() < ob.Modulus();
}
// overload !=
bool operator>(Complex& ob)
{
return this->Modulus() > ob.Modulus();
}
// overload <<
friend istream& operator>>(istream &in, Complex &ob)
{
cout<<"real : ";
in>>ob.real;
cout<<"imaginary : ";
in>>ob.imaginary;
return in;
}
// overload <<
friend ostream& operator<<(ostream &out, Complex &ob)
{
out<<"( "<<ob.real<<" + "<<ob.imaginary<<" i )";
return out;
}
};
int main()
{
Complex ob1;
cin>>ob1;
cout<<"\nob1 : "<<ob1<<"\n\n";
Complex ob2;
cin>>ob2;
cout<<"\nob2 : "<<ob2<<"\n\n";
Complex ob3 = ob1 + ob2;
cout<<"\n\nob1 + ob2 : "<<ob3.toString()<<endl;
Complex ob4 = ob1 - ob2;
cout<<"\n\nob1 - ob2 : "<<ob4.toString()<<endl;
Complex ob5 = ob1 * ob2;
cout<<"\n\nob1 * ob2 : "<<ob5.toString()<<endl;
Complex ob6 = ob1 / ob2;
cout<<"\n\nob1 / ob2 : "<<ob6.toString()<<endl<<endl;
cout<<"\nob1 == ob2 : "<<(ob1==ob2);
return 0;
}
Sample Output

In case of any issue, please leave a comment and I will resolve your issue.
C++ CLASS FOR DEFINING COMPLEX NUMBERS (READ BELOW) Write a C++ defining a class for complex...
USING C++, write a simple class defining complex numbers USING the FRIEND function (READ BELOW) Write a C++ defining a class for complex numbers. A complex number is a number of the form: a + b ∗ i , where, for our purposes, a and b are numbers of type double, and i is a number that represents the quantity √−1. You should represent a complex number here as two values of type double. You should name the variables real...
C++ Complex Class!
Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + imaginaryPart * i where i is Squareroot -1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when ifs declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that...
Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...
C++
Create a class called Complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form realPart + j imaginaryPart Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in case no initializers are provided. Provide public member functions that perform the following tasks: Adding two Complex...
C++ // Need to create a class which contains and operates on complex numbers. A complex number is defined as “a number that can be expressed in the form a + bi, where a and b are real numbers, and i is imaginary” a is called the real part and b is called the imaginary part. The class MyComplexClass will store the complex number as separate real part and imaginary part values. MyComplexClass Private Member Variables *One double for the...
Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....
c++
2) Complex Class A complex number is of the form a+ bi where a and b are real numbers and i 21. For example, 2.4+ 5.2i and 5.73 - 6.9i are complex numbers. Here, a is called the real part of the complex number and bi the imaginary part. In this part you will create a class named Complex to represent complex numbers. (Some languages, including C++, have a complex number library; in this problem, however, you write the...
C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers. is the imaginary unit with the property of . is called the real part of the complex number and is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...
Create a class for working with complex numbers. Only 2 private float members are needed, the real part of the complex number and the imaginary part of the complex number. The following methods should be in your class: a. A default constructor that uses default arguments in case no initializers are included in the main. b. Add two complex numbers and store the sum. c. Subtract two complex numbers and store the difference. d. Multiply two complex numbers and store...
Consider the following C struct that represents a complex number. struct complex { double real; double imaginary; }; (a) [20 points/5 points each] Change this struct into a class. Make the member variables private, and add the following to the class: A default constructor that initializes the real and imaginary parts to 0. A constructor that allows initialization of both real and imaginary parts to any double value. A public member function that returns the magnitude of the complex number....