#include<iostream.h>
template <class T1,class T2>
class complex
{
public:
complex(){}
complex(T1 r,T2 i){real=r;imag=i;}
complex operator+(complex &c);
complex operator-(complex &c);
complex operator*(complex &c);
complex operator/(complex &c);
friend ostream& operator<< (ostream &output,complex &c);
private:
T1 real;
T2 imag;
};
template <class T1,class T2>
complex<T1,T2>::operator * (complex &c)
{
return complex(real*real-imag*c.imag,real*c.imag +imag*c.real );
}
template <class T1,class T2>
complex<T1,T2> ::operator /(complex &c)
{
return complex(real*c.real-imag*c.imag/c.real*c.real+c.imag*c.imag,real*c.imag+imag*c.real/c.real*c.real+c.imag*c.imag);
}
template <class T1,class T2>
complex<T1,T2>::operator-(complex &c)
{
return complex(real-c.real,imag-c.imag );
}
template<class T1,class T2>
complex<T1,T2> ::operator+(complex &c)
{
return complex(real+c.real,imag+c.imag);
}
template<class T1,class T2>
ostream& operator<<(ostream &output,complex &c)
{
if(c.imag<0)
output<<c.real<<c.imag<<'i'<<endl;
else
output<<c.real<<'+'<<c.imag<<'i'<<endl;
return output;
}
void main()
{
complex<int ,int> c1(1,2),c2(2,3),c3;
c3=c1+c2;
cout<<c3;
c3=c1-c2;
cout<<c3;
c3=c1*c2;
cout<<c3;
c3=c1/c2;
cout<<c3;
}
#include<iostream>
using namespace std;
template <class T1,class T2>
class complex
{
public:
complex(){}
complex(T1 r,T2 i){real=r;imag=i;}
complex<T1, T2> operator+(complex<T1, T2> &c);
complex<T1, T2> operator-(complex<T1, T2> &c);
complex<T1, T2> operator*(complex<T1, T2> &c);
complex<T1, T2> operator/(complex<T1, T2> &c);
friend ostream& operator<< (ostream &output,complex<T1, T2> &c)
{
if(c.imag<0)
output<<c.real<<c.imag<<'i'<<endl;
else
output<<c.real<<'+'<<c.imag<<'i'<<endl;
return output;
}
private:
T1 real;
T2 imag;
};
template <class T1,class T2>
complex<T1, T2> complex<T1,T2>::operator * (complex<T1, T2> &c)
{
return complex(real*real-imag*c.imag,real*c.imag +imag*c.real );
}
template <class T1,class T2>
complex<T1, T2> complex<T1,T2> ::operator /(complex<T1, T2> &c)
{
return complex(real*c.real-imag*c.imag/c.real*c.real+c.imag*c.imag,real*c.imag+imag*c.real/c.real*c.real+c.imag*c.imag);
}
template <class T1,class T2>
complex<T1, T2> complex<T1,T2>::operator-(complex<T1, T2> &c)
{
return complex(real-c.real,imag-c.imag );
}
template<class T1,class T2>
complex<T1, T2> complex<T1,T2> ::operator+(complex<T1, T2> &c)
{
return complex(real+c.real,imag+c.imag);
}
void main()
{
complex<int ,int> c1(1,2),c2(2,3),c3;
c3=c1+c2;
cout<<c3;
c3=c1-c2;
cout<<c3;
c3=c1*c2;
cout<<c3;
c3=c1/c2;
cout<<c3;
}