Untitled
unknown
plain_text
a year ago
1.5 kB
15
Indexable
#include <iostream>
template <typename T>
T add(T a, T b) {
return a + b;
}
template <typename T>
class Complex {
private:
T real;
T imaginary;
public:
Complex(T r, T i) : real(r), imaginary(i) {}
void display() const {
if (imaginary < 0) {
std::cout << real << " - " << -imaginary << "i";
} else {
std::cout << real << " + " << imaginary << "i";
}
}
Complex<T> operator+(const Complex<T>& other) const {
return Complex<T>(real + other.real, imaginary + other.imaginary);
}
};
int main() {
using namespace std;
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
int sumInt = add(num1, num2);
cout << "Sum of integers: " << sumInt << endl;
double double1, double2;
cout << "Enter two doubles: ";
cin >> double1 >> double2;
double sumDouble = add(double1, double2);
cout << "Sum of doubles: " << sumDouble << endl;
double real1, imag1, real2, imag2;
cout << "Enter the real and imaginary parts of the first complex number: ";
cin >> real1 >> imag1;
cout << "Enter the real and imaginary parts of the second complex number: ";
cin >> real2 >> imag2;
Complex<double> complex1(real1, imag1);
Complex<double> complex2(real2, imag2);
Complex<double> sumComplex = complex1 + complex2;
cout << "Sum of complex numbers: ";
sumComplex.display();
cout << endl;
return 0;
}Editor is loading...
Leave a Comment