1.
#include <iostream>
using namespace std;
class ComplexNumber {
private:
double real;
double imag;
public:
// Constructor to initialize the complex number
ComplexNumber(double r = 0.0, double i = 0.0) {
real = r;
imag = i;
}
// Function to set the value of the complex number
void setValue(double r, double i) {
real = r;
imag = i;
}
// Function to display the value of the complex number
void showValue() {
if (imag >= 0)
cout << real << " + " << imag << "i" << endl;
else
cout << real << " - " << -imag << "i" << endl;
}
};
int main() {
// Creating an object of ComplexNumber class
ComplexNumber num;
// Setting and showing the value of a complex number
[Link](3.5, 2.0);
cout << "Complex Number: ";
[Link]();
return 0;
}
1.b
#include <iostream>
using namespace std;
class ComplexNumber {
private:
double real;
double imag;
public:
// Constructor to initialize the complex number
ComplexNumber(double r = 0.0, double i = 0.0) {
real = r;
imag = i;
}
// Function to set the value of the complex number
void setValue(double r, double i) {
real = r;
imag = i;
}
// Function to display the value of the complex number
void showValue() const {
if (imag >= 0)
cout << real << " + " << imag << "i";
else
cout << real << " - " << -imag << "i";
}
// Function to add two complex numbers
ComplexNumber add(const ComplexNumber& other) const {
ComplexNumber result;
[Link] = real + [Link];
[Link] = imag + [Link];
return result;
}
// Function to subtract two complex numbers
ComplexNumber subtract(const ComplexNumber& other) const {
ComplexNumber result;
[Link] = real - [Link];
[Link] = imag - [Link];
return result;
}
// Function to multiply two complex numbers
ComplexNumber multiply(const ComplexNumber& other) const {
ComplexNumber result;
[Link] = (real * [Link]) - (imag * [Link]);
[Link] = (real * [Link]) + (imag * [Link]);
return result;
}
};
int main() {
// Creating objects of ComplexNumber class
ComplexNumber num1(3.5, 2.0);
ComplexNumber num2(1.5, 4.0);
// Displaying the first complex number
cout << "First complex number: ";
[Link]();
cout << endl;
// Displaying the second complex number
cout << "Second complex number: ";
[Link]();
cout << endl;
// Performing addition
ComplexNumber sum = [Link](num2);
cout << "Sum: ";
[Link]();
cout << endl;
// Performing subtraction
ComplexNumber diff = [Link](num2);
cout << "Difference: ";
[Link]();
cout << endl;
// Performing multiplication
ComplexNumber product = [Link](num2);
cout << "Product: ";
[Link]();
cout << endl;
return 0;
}
1c…
#include <iostream>
using namespace std;
class ComplexNumber {
private:
double real;
double imag;
public:
// Constructor to initialize the complex number
ComplexNumber(double r = 0.0, double i = 0.0) {
real = r;
imag = i;
}
// Function to set the value of the complex number
void setValue(double r, double i) {
real = r;
imag = i;
}
// Function to display the value of the complex number
void showValue() const {
if (imag >= 0)
cout << real << " + " << imag << "i";
else
cout << real << " - " << -imag << "i";
}
// Function to add two complex numbers
ComplexNumber add(const ComplexNumber& other) const {
ComplexNumber result;
[Link] = real + [Link];
[Link] = imag + [Link];
return result;
}
// Function to subtract two complex numbers
ComplexNumber subtract(const ComplexNumber& other) const {
ComplexNumber result;
[Link] = real - [Link];
[Link] = imag - [Link];
return result;
}
// Function to multiply two complex numbers
ComplexNumber multiply(const ComplexNumber& other) const {
ComplexNumber result;
[Link] = (real * [Link]) - (imag * [Link]);
[Link] = (real * [Link]) + (imag * [Link]);
return result;
}
// Function to multiply the complex number by a scalar (real) value
ComplexNumber multiply(double scalar) const {
ComplexNumber result;
[Link] = real * scalar;
[Link] = imag * scalar;
return result;
}
};
int main() {
// Creating an object of ComplexNumber class
ComplexNumber num(3.5, 2.0);
// Displaying the original complex number
cout << "Original complex number: ";
[Link]();
cout << endl;
// Multiplying the complex number by a scalar
double scalar = 2.5;
ComplexNumber scaled = [Link](scalar);
// Displaying the scaled complex number
cout << "Scaled complex number (multiplied by " << scalar << "): ";
[Link]();
cout << endl;
return 0;
}