0% found this document useful (0 votes)
5 views3 pages

OOP 1 Complex Numbers

The document provides a C++ implementation of a Complex class that represents complex numbers. It includes a default constructor, operator overloading for addition and multiplication, and stream operators for input and output. The main function demonstrates reading two complex numbers, calculating their sum and product, and displaying the results.

Uploaded by

tejasvi1907
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)
5 views3 pages

OOP 1 Complex Numbers

The document provides a C++ implementation of a Complex class that represents complex numbers. It includes a default constructor, operator overloading for addition and multiplication, and stream operators for input and output. The main function demonstrates reading two complex numbers, calculating their sum and product, and displaying the results.

Uploaded by

tejasvi1907
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

1. Implement a class Complex which represents the Complex Number data type.

Implement the following


1. Constructor (including a default constructor which creates the complex number
0+0i).
2. Overload operator+ to add two complex numbers.
3. Overload operator* to multiply two complex numbers.
4. Overload operators << and >> to print and read Complex Numbers.

#include<iostream>

using namespace std;

class Complex {

private:

double real;

double imag;

public:

// Default constructor

Complex() {

real = 0;

imag = 0;

// Parametrized constructor

Complex(double r, double i) {

real = r;

imag = i;

// Overloading + operator

Complex operator +(const Complex& obj) {

// Return a new Complex object with sum of real and imaginary parts

return Complex(real + [Link], imag + [Link]);

// Overloading * operator

Complex operator *(const Complex& obj) {

double r = real * [Link] - imag * [Link]; // Real part


double i = real * [Link] + imag * [Link]; // Imaginary part

return Complex(r, i);

// Overloading << operator

friend ostream& operator <<(ostream& out, const Complex& c) {

if ([Link] >= 0) {

out << [Link] << "+" << [Link] << "i"; // Only "out" here, not cout

} else {

out << [Link] << "-" << -[Link] << "i";

return out; // Return the stream for chaining

// Overloading >> operator

friend istream& operator >>(istream& in, Complex& c) {

in >> [Link] >> [Link]; // Read real and imaginary parts

return in; // Return the stream for chaining

};

int main() {

Complex c1, c2, sum, product;

// Read two complex numbers

cout << "Enter the first complex number (real and imaginary): ";

cin >> c1;

cout << "Enter the second complex number (real and imaginary): ";

cin >> c2;

// Add two complex numbers

sum = c1 + c2;

cout << "Sum = " << sum << endl;


// Multiply two complex numbers

product = c1 * c2;

cout << "Product = " << product << endl;

return 0;

You might also like