Create a C++ program to calculate the subtraction of two complex numbers by overloading the '-' binary operator.Complex numbers are the numbers that are expressed in the form of a+ib where, a,b are real numbers and 'i' is an imaginary number called “iota”. The value of i = (√-1). For example, 10+3i is a complex number, where 10 is a real number and 3i is an imaginary number .Sample Input// Read the real and imaginary parts of the first complex number59// Read the real and imaginary parts of the second complex number37Sample OutputNumber1:5+9iNumber2:3+7iAnswer is 2-2i
Question
Create a C++ program to calculate the subtraction of two complex numbers by overloading the '-' binary operator.Complex numbers are the numbers that are expressed in the form of a+ib where, a,b are real numbers and 'i' is an imaginary number called “iota”. The value of i = (√-1). For example, 10+3i is a complex number, where 10 is a real number and 3i is an imaginary number .Sample Input// Read the real and imaginary parts of the first complex number59// Read the real and imaginary parts of the second complex number37Sample OutputNumber1:5+9iNumber2:3+7iAnswer is 2-2i
Solution 1
Sure, here is a simple C++ program that overloads the '-' operator to subtract two complex numbers:
#include<iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r = 0, int i =0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator - (Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imag = imag - obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main()
{
Complex c1(5, 9), c2(3, 7);
Complex c3 = c1 - c2;
cout << "Number1: ";
c1.print();
cout << "Number2: ";
c2.print();
cout << "Answer is ";
c3.print();
}
This program first defines a class Complex to represent complex numbers. The class has two data members real and imag to store the real and imaginary parts of the complex number. The class also has a member function operator - to overload the '-' operator for subtraction of complex numbers. The function subtracts the real and imaginary parts of the complex numbers separately.
In the main function, we create two Complex objects c1 and c2 with the given real and imaginary parts. We then subtract c2 from c1 using the overloaded '-' operator and store the result in c3. Finally, we print the complex numbers and the result.
Solution 2
Here is a simple C++ program that overloads the '-' operator to subtract two complex numbers:
#include<iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r = 0, int i =0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator - (Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imag = imag - obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main()
{
Complex c1(5, 9), c2(3, 7);
Complex c3 = c1 - c2;
cout << "Number1: ";
c1.print();
cout << "Number2: ";
c2.print();
cout << "Answer is ";
c3.print();
}
In this program, we define a class Complex to represent complex numbers. We overload the '-' operator in this class to perform subtraction of complex numbers. The overloaded operator function subtracts the real and imaginary parts of the right-hand side complex number from the real and imaginary parts of the left-hand side complex number respectively. The result is returned as a new Complex object.
In the main function, we create two Complex objects c1 and c2 and subtract them using the '-' operator. The result is stored in c3 and then printed.
Solution 3
Sure, here is a simple C++ program that overloads the '-' operator to subtract two complex
Solution 4
Here is a simple C++ program that overloads the '-' operator to subtract two complex numbers:
#include<iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r = 0, int i =0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator - (Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imag = imag - obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main()
{
Complex c1(5, 9), c2(3, 7);
Complex c3 = c1 - c2;
cout << "Number1: ";
c1.print();
cout << "Number2: ";
c2.print();
cout << "Answer is ";
c3.print();
}
In this program, we define a class Complex to represent complex numbers. We overload the '-' operator in this class to perform subtraction of complex numbers. The operator - function subtracts the real and imaginary parts of the complex numbers separately and returns the result as a new Complex object. In the main function, we create two Complex objects c1 and c2, subtract them using the '-' operator, and print the result.
Solution 5
Here is a simple C++ program that overloads the '-' operator to subtract two complex numbers:
#include<iostream>
using namespace std;
class Complex {
public:
int real, imag;
Complex(int r = 0, int i =0) {real = r; imag = i;}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator - (Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imag = imag - obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};
int main()
{
Complex c1(5, 9), c2(3, 7);
Complex c3 = c1 - c2;
cout << "Number1: ";
c1.print();
cout << "Number2: ";
c2.print();
cout << "Answer is ";
c3.print();
}
In this program, we define a class Complex to represent complex numbers. We overload the '-' operator in this class to perform subtraction of complex numbers. The operator - function subtracts the real and imaginary parts of the complex numbers separately and returns the result as a new Complex object. The print function is used to print a Complex object in the form a + ib. In the main function, we create two Complex objects c1 and c2, subtract them using the '-' operator, and print the result.
Similar Questions
Write a program to overload arithmetic minus operator for subtracting two complex numbers (use constructors also).
Create a C++ programme that accomplishes all arithmetic operations on two complex objects, including addition, subtraction, multiplication, and division. (X + i * Y) represents the Complex Object (Where X and Y are two real numbers and 'i' is an Imaginary). As a programmer, create a class called "COMPLEX" containing the data members X and Y and also getData() and putData() methods to pefrom read and write for the above said Data Members. Define a separate functions for the operations such as Addition, Subtraction, Multiplication, and Division.Input :Read the Two Complex Objects (Each with X and Y)Output :Addition of Two Complex ObjectsSubtraction of Two Complex ObjectsMultiplication of Two Complex ObjectsDivision of Two Complex ObjectsNote :Both X and Y are Non-Negative, otherwise print "Invalid"Define a separate functions with return type and with arguments of two complext objects.
operations on complex numbers Write a program to find out the addition and multiplication of two complex numbers using structures and functions. Note: Write the code in the functions readC(), display(), add() and product().
Create a Complex class to represent complex numbers. Overload the addition (+), subtraction (-), multiplication (*), and division (/) operators for complex numbers. The prompt ask 2 complex number from the user and display the respective output for the user input.
Write a program overloading arithmetic operators to add two complex numbers using oops c++
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.