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

OOOP1

this is experiment no .01 for sppu aids see codes

Uploaded by

gameralexander61
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)
20 views2 pages

OOOP1

this is experiment no .01 for sppu aids see codes

Uploaded by

gameralexander61
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

#include <iostream>

using namespace std;

class Complex {
float x; // Real part
float y; // Imaginary part

public:
// Default constructor
Complex() : x(0), y(0) {}

// Parameterized constructor
Complex(float real, float imag) : x(real), y(imag) {}

// Overloaded + operator
Complex operator+(const Complex& c) const {
return Complex(x + c.x, y + c.y); // Adds real and imaginary parts
}

// Overloaded * operator
Complex operator*(const Complex& c) const {
return Complex((x * c.x) - (y * c.y), (y * c.x) + (x * c.y)); // Multiplies complex numbers
}

// Friend function for input


friend istream& operator>>(istream& input, Complex& t) {
cout << "Enter the real part: ";
input >> t.x;
cout << "Enter the imaginary part: ";
input >> t.y;
return input;
}

// Friend function for output


friend ostream& operator<<(ostream& output, const Complex& t) {
output << t.x;
if (t.y >= 0)
output << "+" << t.y << "i"; // Displays positive imaginary part
else
output << t.y << "i"; // Displays negative imaginary part
return output;
}
};
int main() {
Complex c1, c2, c3, c4;

cout << "Default constructor value:\n";


cout << c1 << endl; // Displays 0+0i

cout << "\nEnter the 1st number\n";


cin >> c1; // Input for first complex number

cout << "\nEnter the 2nd number\n";


cin >> c2; // Input for second complex number

c3 = c1 + c2; // Addition
c4 = c1 * c2; // Multiplication

cout << "\nThe first number is " << c1 << endl;


cout << "The second number is " << c2 << endl;
cout << "The addition is " << c3 << endl;
cout << "The multiplication is " << c4 << endl;

return 0;
}

OUTPUT:-
Default constructor value:
0+0i

Enter the 1st number


Enter the real part: 10
Enter the imaginary part: 5

Enter the 2nd number


Enter the real part: 3
Enter the imaginary part: 4

The first number is 10+5i


The second number is 3+4i
The addition is 13+9i
The multiplication is 2+70i

You might also like