Question: Four integers are read from input, where the first two integers are the real and imaginary parts of complexNumber 1 and the second two integers

Four integers are read from input, where the first two integers are the real and imaginary parts of complexNumber1 and the second two integers are the real and imaginary parts of complexNumber2. Define two functions to overload the - operator. The first function overloads the - operator to subtract a complex number and an integer representing the real part of the complex number. The second function overloads the - operator to subtract two complex numbers.
Ex: If the input is 101253, then the output is:
(10+12i)-5=5+12i
(10+12i)-(5+3i)=5+9i
Note:
A complex number consists of a real part and an imaginary part. Ex: 10+12i, where 10 is the real part and 12 is the imaginary part.
The difference of a complex number and an integer representing the real part of the complex number, (10+12i)-5, is:
the difference of the real part of the complex number and the integer, (10-5)
the imaginary part is unchanged
Thus, the difference is 5+12i.
The difference of two complex numbers, (10+12i) and (5+3i), is the difference of their real parts (10-5) and the difference of their imaginary parts (12i -3i). Thus, the difference is 5+9i. CODE: #include
using namespace std;
class ComplexNumber {
public:
ComplexNumber(int realPart =0, int imaginaryPart =0);
void Print() const;
ComplexNumber operator-(int rhs);
ComplexNumber operator-(ComplexNumber rhs);
private:
int real;
int imaginary;
};
ComplexNumber::ComplexNumber(int realPart, int imaginaryPart){
real = realPart;
imaginary = imaginaryPart;
}
// No need to accommodate for overflow or negative values
/* Your code goes here */
void ComplexNumber::Print() const {
cout real "+" imaginary "i";
}
int main(){
int realPart1;
int imaginaryPart1;
int realPart2;
int imaginaryPart2;
cin >> realPart1;
cin >> imaginaryPart1;
cin >> realPart2;
cin >> imaginaryPart2;
ComplexNumber complexNumber1(realPart1, imaginaryPart1);
ComplexNumber complexNumber2(realPart2, imaginaryPart2);
ComplexNumber difference1= complexNumber1- realPart2;
ComplexNumber difference2= complexNumber1- complexNumber2;
cout "(";
complexNumber1.Print();
cout ")-" realPart2"=";
difference1.Print();
cout endl;
cout endl;
cout "(";
complexNumber1.Print();
cout ")-(";
complexNumber2.Print();
cout ")=";
difference2.Print();
cout endl;
return 0;
}
 Four integers are read from input, where the first two integers

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!