6.
‘C++’ program to perform addition of two complex numbers using constructor
#include <iostream>
using namespace std;
class Complex {
private:
float real, imag;
public:
// Constructor
Complex(float r = 0, float i = 0) {
real = r;
imag = i;
}
// Function to add two complex numbers
Complex add(Complex c) {
Complex temp;
[Link] = real + [Link];
[Link] = imag + [Link];
return temp;
}
// Function to display result
void display() {
cout << real << " + " << imag << "i";
}
};
int main() {
Complex c1(2.5, 3.5); // First complex number
Complex c2(1.5, 2.5); // Second complex number
Complex result;
result = [Link](c2);
cout << "Sum of complex numbers: ";
[Link]();
return 0;
}
Output: Sum of complex numbers: 4 + 6i