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

6 Program Lab

The document presents a C++ program that defines a 'Complex' class for performing addition of two complex numbers using a constructor. It includes methods for adding complex numbers and displaying the result. The main function demonstrates the addition of two complex numbers and outputs their sum as '4 + 6i'.

Uploaded by

lkhgfds751
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)
14 views2 pages

6 Program Lab

The document presents a C++ program that defines a 'Complex' class for performing addition of two complex numbers using a constructor. It includes methods for adding complex numbers and displaying the result. The main function demonstrates the addition of two complex numbers and outputs their sum as '4 + 6i'.

Uploaded by

lkhgfds751
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

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

You might also like