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

PLEASE USE C++ THANKS 9.5 (complex Class) Create a class called complex for perf

ID: 2246622 • Letter: P

Question

PLEASE USE C++ THANKS

9.5 (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 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 in case no initializers are provided. Provide public member functions that perform the following tasks a. Adding two complex numbers: The real parts are added together and the imaginary parts are added together. b. Subtracting two complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. c. Printing complex numbers in the form (a, b), where a is the real part and b is the imaginary part.

Explanation / Answer

#include<iostream>
using namespace std;

class Complex {
        double realPart;
        double imaginaryPart;
    public:
        Complex(double r=0,double i=0)
       {
           realPart = r;
           imaginaryPart = i;
       }
        void add(Complex, Complex);
        void subtract(Complex, Complex);
        void print();
};

void Complex::add (Complex op1, Complex op2) {
    realPart= op1.realPart+op2.realPart;
    imaginaryPart = op1.imaginaryPart+op2.imaginaryPart;
}

void Complex::subtract (Complex op1, Complex op2) {
     realPart = op1.realPart-op2.realPart;
     imaginaryPart = op1.imaginaryPart-op2.imaginaryPart;
}

void Complex::print () {
    cout << "RealPart      : " << this->realPart << endl;
    cout<< "ImaginaryPart : " << this->imaginaryPart << endl;
}

int main () {
    Complex operand1(5,4), operand2(3,2), result;
    result.add(operand1, operand2);
   cout << "The sum is "<<endl;
   result.print();// << endl;

    result.subtract(operand1, operand2);
   cout << "The difference is "<<endl;
   result.print();// << endl;


   return 0;
}

OUTPUT:

The sum is
RealPart      : 8
ImaginaryPart : 6
The difference is
RealPart      : 2
ImaginaryPart : 2

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