0% found this document useful (0 votes)
3 views2 pages

Complex Overloading

The document contains a C++ program that defines a Complex class for performing arithmetic operations on complex numbers. It includes methods for addition, subtraction, multiplication, and division, as well as a display function to output the results. The main function demonstrates these operations with two complex numbers, producing results for each operation.

Uploaded by

anises208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Complex Overloading

The document contains a C++ program that defines a Complex class for performing arithmetic operations on complex numbers. It includes methods for addition, subtraction, multiplication, and division, as well as a display function to output the results. The main function demonstrates these operations with two complex numbers, producing results for each operation.

Uploaded by

anises208
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

//Name: Mayur Machhindra Rode

//Roll No: 112

#include<iostream>
using namespace std;
class Complex {
private:
int real;
int imag;
public:
Complex(int r=0, int i=0) {
real = r;
imag = i;
}
Complex operator + (Complex const &obj) {
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
Complex operator - (Complex const &obj) {
Complex res;
res.real = real - obj.real;
res.imag = imag - obj.imag;
return res;
}
Complex operator * (Complex const &obj) {
Complex res;
res.real = real * obj.real - imag * obj.imag;
res.imag = real * obj.imag + imag * obj.real;
return res;
}
Complex operator / (Complex const &obj) {
Complex res;
int denom = obj.real * obj.real + obj.imag * obj.imag;
res.real = (real * obj.real + imag * obj.imag) / denom;
res.imag = (imag * obj.real - real * obj.imag) / denom;
return res;
}
void display() {
cout << real << " + i" << imag << endl;
}
};
int main() {
Complex c1(3, 7), c2(4, 8);
Complex c3 = c1 + c2;
Complex c4 = c1 - c2;
Complex c5 = c1 * c2;
Complex c6 = c1 / c2;

cout << "Addition: ";


c3.display();
cout << "Subtraction: ";
c4.display();
cout << "Multiplication: ";
c5.display();
cout << "Division: ";
c6.display();
return 0;
}

>> OUTPUT

Addition: 7 + i15
Subtraction: -1 + i-1
Multiplication: -44 + i52
Division: 0 + i0

You might also like