complex number addition and multiplication using friend function
#include<iostream>
using namespace std;
class Complex
{
int real,img;
public:
friend istream &operator>>(istream &in , Complex &c)
{
in>>c.real>>c.img;
return in;
}
friend ostream &operator<<(ostream &out,Complex &c)
{
out<<c.real<<"+"<<c.img<<"i";
}
Complex operator+(Complex c2);
Complex operator*(Complex c2);
};
Complex Complex::operator+(Complex c2)
{
Complex temp;
temp.real=real+c2.real;
temp.img=img+c2.img;
return (temp);
}
Complex Complex::operator*(Complex c2)
{
Complex temp;
temp.real=real*c2.real;
temp.img=img*c2.img;
return (temp);
}
int main()
{
Complex c1,c2,c3,c4;
cout<<"enter the real and imag :=";
cin>>c1;
cout<<"enter the real and imag :=";
cin>>c2;
cout<<"\n display the first complex:=";
cout<<c1;
cout<<"\n display the second complex:=";
cout<<c2;
c3=Complex(c1+c2);
c4=Complex(c1*c2);
cout<<"\n addition is:=";
cout<<c3;
cout<<"\n multiplication is=";
cout<<c4;
c3.operator+(c2);
c4.operator*(c2);
return 0;
}
using namespace std;
class Complex
{
int real,img;
public:
friend istream &operator>>(istream &in , Complex &c)
{
in>>c.real>>c.img;
return in;
}
friend ostream &operator<<(ostream &out,Complex &c)
{
out<<c.real<<"+"<<c.img<<"i";
}
Complex operator+(Complex c2);
Complex operator*(Complex c2);
};
Complex Complex::operator+(Complex c2)
{
Complex temp;
temp.real=real+c2.real;
temp.img=img+c2.img;
return (temp);
}
Complex Complex::operator*(Complex c2)
{
Complex temp;
temp.real=real*c2.real;
temp.img=img*c2.img;
return (temp);
}
int main()
{
Complex c1,c2,c3,c4;
cout<<"enter the real and imag :=";
cin>>c1;
cout<<"enter the real and imag :=";
cin>>c2;
cout<<"\n display the first complex:=";
cout<<c1;
cout<<"\n display the second complex:=";
cout<<c2;
c3=Complex(c1+c2);
c4=Complex(c1*c2);
cout<<"\n addition is:=";
cout<<c3;
cout<<"\n multiplication is=";
cout<<c4;
c3.operator+(c2);
c4.operator*(c2);
return 0;
}
please comment
ReplyDeleteto improve my skill