#include <iostream>
using namespace std;
class Complex
{
private:
float real;
float imag;
public:
Complex(float r = 0, float i = 0):real(r),imag(i){};
void setComplex(void)
{
cout << "Enter the real and imaginary parts : ";
cin >> this->real;
cin >> this->imag;
}
Complex add(const Complex& c)
{
Complex comp;
comp.real = this->real + c.real;
comp.imag = this->imag + c.imag;
return comp;
}
Complex subtract(const Complex& c)
{
Complex comp;
comp.real = this->real - c.real;
comp.imag = this->imag - c.imag;
return comp;
}
Complex multiply(const Complex&c)
{
Complex comp;
comp.real=this->real*c.real;
comp.imag=this->imag*c.imag;
return comp;
}
void square()
{
float temp=this->real;
this->real=this->real*this->real-this->imag*this->imag;
this->imag=2*this->imag*temp;
}
void printComplex(void)
{
if ( this->imag >= 0 )
cout << "Complex number after operation: " <<
this->real << " + " << this->imag <<
"i"<<endl;
else
cout << "Complex number after operation: " <<
this->real << " " <<
this->imag<<"i"<<endl;
}
};
int main()
{
Complex a, b, c, d,e;
cout << "Setting first complex number " <<
endl;
a.setComplex();
cout << "Setting second complex number " << endl;
b.setComplex();
/* Adding two complex numbers */
cout << "Addition of a and b : " << endl;
c = a.add(b);
c.printComplex();
/* Subtracting two complex numbers */
cout << "Subtraction of a and b : " << endl;
d = a.subtract(b);
d.printComplex();
/* Multiplication two complex numbers */
cout << "Multiplication of a and b : " << endl;
e = a.multiply(b);
e.printComplex();
/*change the complex number to its square*/
cout << "Changing complex number a to its square: " <<
endl;
a.square();
a.printComplex();
}

Create a class for working with complex numbers. Only 2 private float members are needed, the...
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++ 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...
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++ 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...
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++
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...
Write a class called "Complex". The class has two private data members which correspond to the real and imaginary parts of a complex number. .The class Complex should have several (methods) functions: set the values, display the number in real/imaginary (re + j*im) format, and display the number in polar (r, theta) format. The main program should declare two objects of type Complex. It should set the first object to 3 +j2.5 and the second object to 2.7-j6.8. The program...
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...
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...
Create the header file named “Complex.h” that contains the following class: The class Complex represents a complex number which is a number of the form a + bi where a and b are real numbers and i2 = −1. The class should contain: Private double field named real. Private double field named imaginary. Public default constructor that assigns 1 to real and 0 to imaginary. Public overloaded constructor that takes a double as a parameter named real. It assigns real...