- A fn to get the CN from user.
- A fn to display the CN
- A fn to add the two CN which are accepted as parameters.
- Multiply
- Subtract
- Divide
Method II
Addition of two CN.
#include
class complex
{
float r, i ;
public:
void getcomplex ( );
void display ( );
complex add (complex);
complex subtract (complex);
complex multiply (complex);
complex divide (complex);
};
void complex :: get complex ( )
{
cin>>r>>i;
}
void complex :: display ( ).
{
cout< } complex complex :: add (complex c) { complex temp; temp.r = r+c.r; temp.i = i+c.i; return temp ; } complex complex :: multiply (complex c) { complex temp; temp.r = r * c.r – i * c.i ; temp.i = i * c.r + r* c.i ; return temp ; } complex complex :: divide (complex c) { temp.r = (r*c.r + i * c.i) / ( c.r*c.r+c.i+c.i) temp.i = (1*c.r – r* c.1) / (c.r*c.r+c.i*c.i) ; return temp ; } complex complex :: subtract (complex) { temp.r = r – c.r ; temp.i = i – c.i ; return temp ; } void main ( ) { complex c1, c2, a, s, m, d ; cout<<"Enter CN = \n" ; c1.getcomplex ( ) ; cout<<"Enter CN = \n" ; c2.get complex ( ); a = c1.add (c2) ; s= c2.sbutract (c2) ; m = c1.multiply (c2) ; d = c1.divide (c2) ; cout<<"Addition=\n" ; a.display ( ); cout<<"subtraction=\n"; a.display ( ) ; cout<<"multiplication = \n" ; m.display ( ) ; cout<<"division=\n" ; d.display ( ); }