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

C++ Define a class for complex numbers. A complex number is a number of the form

ID: 3795644 • Letter: C

Question

C++ Define 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 2-1 . Represent a complex number as two values www.itpub.net Programming Projects 365 of type double . Name the member variables real and imaginary . (The variable for the number that is multiplied by i is the one called imaginary .) Call the class Complex . Include a constructor with two parameters of type double that can be used to set the member variables of an object to any values. Include a constructor that has only a single parameter of type double ; call this parameter realPart and define the constructor so that the object will be initialized to realPart + 0*i . Include a default constructor that initializes an object to 0 (that is, to 0 + 0*i ). Overload all the following operators so that they correctly apply to the type Complex : == , + , - , * , >> , and << . You should also write a test program to test your class. Hints: To add or subtract two complex numbers, add or subtract the 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 In the interface file, you should define a constant i as follows: const Complex i(0, 1); This defined constant i will be the same as the i discussed above. C++ QUESTION

Explanation / Answer

//complex_numbers.cpp

#include <iostream>
using namespace std;

class Complex
{
private:
double real;
double imaginary;
  
public:
Complex(double realPart , double imagPart)
{
real = realPart ;
imaginary = imagPart;
}
  
  
Complex(double realPart)
{
real = realPart ;
imaginary = 0;
}
  
Complex()
{
real = 0;
imaginary = 0;
}
  
Complex operator+(const Complex& b) {
Complex c;
c.real = this->real + b.real;
c.imaginary = this->imaginary + b.imaginary;
return c;
}
  
Complex operator-(const Complex& b) {
Complex c;
c.real = this->real - b.real;
c.imaginary = this->imaginary - b.imaginary;
return c;
}
  
Complex operator*(const Complex& b) {
Complex c;
c.real = this->real*b.real - this->imaginary*b.imaginary;
c.imaginary = this->real*b.imaginary + this->imaginary*b.real;
return c;
}
  
bool operator==(const Complex& b) {
return (this->real == b.real) && (this->imaginary == b.imaginary);
}
  
friend ostream &operator << (ostream &os, Complex &t) {
os << t.real << " + " << t.imaginary << "*i" << endl;
return os;
}
  
friend istream & operator >> (istream &in, Complex &c)
{
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imagenory Part ";
in >> c.imaginary;
return in;
}
};

// Tester program

#include <iostream>
#include "complex_numbers.cpp"

using namespace std;

int main()
{
Complex c1;
Complex c2;
  
cin >> c1;
cin >> c2;

cout << c1;
cout << c2;

Complex c3;
c3 = c1+c2;

cout << c3;

c3 = c1-c2;
cout << c3;

c3 = c1*c2;
cout << c3;

if (c1 == c2)
{
cout << "Equal ";
}   
else
{
cout <<"Not equal ";
}
}

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