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

Create a class to perform addition operation on complex data. The program should

ID: 3912168 • Letter: C

Question

Create a class to perform addition operation on complex data. The program should ask for real and imaginary part of two complex numbers, and display the real and imaginary parts of their sum.
Note:
Declare a class with two private double type member variables for the real and imaginary parts respectively. Define a constructor taking two arguments of doubles for the real and imaginary parts, respectively. Define two functions named get_rl and get_im for returning the real and imaginary parts respectively.
In the main function, input two complex numbers, each with real and imaginary parts respectively, e.g. nu1 and nu2 for the first complex number (comp1) and nu3 and nu4 for the second complex number (comp2). Build two complex number objects, and perform addition operation, by adding their real and imaginary parts (double real = comp1.get_rl() +comp2.get_rl();double imagin = comp1.get_im() + comp2.get_im();), and then print the sum of the two complex numbers (print real and imagin respectively).

add a public member function named add to class Complex. The add function takes an argument of Complex number and add the number to the current complex number, and return the result (a complex number). Test the add function in the main function (Complex com3 = comp1.add(comp2);), and print the real and imaginary parts of com3.


Define another add function to the class Complex, which takes two arguments of complex numbers and returns the sum of these two complex numbers.
add overloaded binary operators +, -, and +=. Delete both add functions.

Explanation / Answer


Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you

#include <iostream>
#include <string>
using namespace std;

class Complex
{
private:
double real;
double imaginary;
public:
Complex(double r, double im)
{
real = r;
imaginary = im;
}

double get_rl()
{
return real;
}

double get_im()
{
return imaginary;
}

/*Complex add(const Complex &other)
{
return Complex(real + other.real, imaginary + other.imaginary);
}*/

Complex operator +(const Complex &other)
{
return Complex(real + other.real, imaginary + other.imaginary);
}

Complex operator -(const Complex &other)
{
return Complex(real - other.real, imaginary - other.imaginary);
}

Complex operator +=(const Complex &other)
{
*this = *this + other;
return *this;
}

};

int main(){
double nu1, nu2, nu3, nu4;

cout << "Enter the real and imaginary parts for first complex number: ";
cin >> nu1 >> nu2;

cout << "Enter the real and imaginary parts for second complex number: ";
cin >> nu3 >> nu4;

Complex comp1(nu1, nu2), comp2(nu3, nu4);

double real = comp1.get_rl() +comp2.get_rl();
double imagin = comp1.get_im() + comp2.get_im();

cout << "The sum has real = " << real << ", imaginary = " << imagin << endl;

Complex sum = comp1 + comp2;
Complex diff = comp1 - comp2;

cout << "The sum using operator + has real = " << sum.get_rl() << ", imaginary = " << sum.get_im() << endl;
cout << "The difference using operator - has real = " << diff.get_rl() << ", imaginary = " << diff.get_im() << endl;

cout << "Using the += operator " << endl;
comp1 += comp2;
cout << "now first complex number has real = " << comp1.get_rl() << ", imaginary = " << comp1.get_im() << endl;


}

output
-----
Enter the real and imaginary parts for first complex number: 2 4
Enter the real and imaginary parts for second complex number: 5 8
The sum has real = 7, imaginary = 12
The sum using operator + has real = 7, imaginary = 12
The difference using operator - has real = -3, imaginary = -4
Using the += operator
now first complex number has real = 7, imaginary = 12

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