Write a program to overload arithmetic minus operator for subtracting two complex numbers (use constructors also).
Question
Write a program to overload arithmetic minus operator for subtracting two complex numbers (use constructors also).
Solution
Sure, here is a Python program that overloads the arithmetic minus operator for subtracting two complex numbers using constructors:
class Complex:
def __init__(self, real, imag):
self.real = real
self.imag = imag
# Overloading the '-' operator
def __sub__(self, other):
return Complex(self.real - other.real, self.imag - other.imag)
def __str__(self):
return f"{self.real} - {self.imag}i"
# Creating two complex numbers
c1 = Complex(3, 2)
c2 = Complex(1, 2)
# Subtracting two complex numbers using overloaded '-' operator
c3 = c1 - c2
print("The subtraction of two complex numbers is", c3)
In this program, we define a class Complex to represent complex numbers. We define a constructor __init__ to initialize the real and imaginary parts of a complex number. We overload the - operator by defining a method __sub__ that returns a new Complex object representing the difference of two complex numbers. The __str__ method is used to represent the Complex object as a string.
Similar Questions
Write a program overloading arithmetic operators to add two complex numbers using oops c++
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
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.
Write a Java program to find the addition, subtraction, multiplication, and division of two complex numbers using inheritance. Create an abstract class called Complex. From this extends the child classes Addition, Subtraction, Multiplication, and Division. All of these child classes should contain the same methods.Input format :The input consists of four double-type variables, which denote the real and imaginary parts of the first and second complex numbers, respectively.Output format :The output consists of the addition, subtraction, multiplication, and division of the two complex numbers.Refer to the sample input and output for format specifications.Sample test cases :Input 1 :2 64 2Output 1 :Addition: 6.0000 +8.0000 iSubtraction: -2.0000 +4.0000 iMultiplication: -4.0000 +28.0000 iDivision: 1.0000 +1.0000 i
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.
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.