Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

Students also viewed these Databases questions

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago