Write a program to design a class
representing complex numbers and having functionality of performing addition
and multiplication of two complex numbers using operator overloading.
#include <iostream>
using namespace std;
class Complex{
float real, imaginary;
public:
Complex(){}
Complex(float r, float i): real(r), imaginary(i){}
void print(){
cout<<this->real;
if(imaginary != 0) cout<<" + "<<"i";
if(imaginary < 0) cout<<"("<<imaginary<<")";
else cout<<imaginary;
}
//multiplication
Complex operator*(const Complex &other){
return Complex(this->real * other.real + this->imaginary * other.imaginary, this->real * other.imaginary + this->imaginary * other.real);
}
//addition
Complex operator+(const Complex &other){
return Complex(this->real + other.real, this->imaginary + other.imaginary);
}
};
Comments
Leave a comment