Develop a class to represent complex number. The class should have following member function.
  1. A fn to get the CN from user.
  2. A fn to display the CN
  3. A fn to add the two CN which are accepted as parameters.
  4. Multiply
  5. Subtract
  6. Divide
   
Write suitable main fn.
Program :-
#include
class complex
{
float r, i ;
Public:
void getcomplex ( ) ;
void display ( ) ;
void add (complex, complex) ;
void subtract ( complex, complex) ;
void multiply ( complex, complex) ;
void divide ( complex, complex) ;
};
void complex :: get complex ( )
{
cin>>r>>i;
}
void complex :: display ( )
{
cout<
}
void complex :: add (complex x, complex y)
{
r=x.r+y.r ;
i=x.i+y.i ;
}
void complex : subtract (complex x, complex y)
{
r= x.r – y.r ;
i= x.i – y.i ;
}
 
void complex : multiply (complex x, complex y)
{
r= x.r * y.r – x.i * y.i ;
i = x.i * y.r + x.r * y.i ;
}
void complex :: divide (complex x, complex y)
{
r=(x.r*y.r+x.i*yi) / (y.r*y.r+y.i+y.i) ;
i=(x.i*y.r –x.r*y.i) / (y.r*y.r+y.i*y.i) ;
}
Logic:-        x.r x.i                   y.r y.i
(a+ib)                   (c+id) *
=   (ac-bd) + i (bc + ad)
a+ib
c+id
=       a+ib .         c−id
c+id            c−id *
=       (ac+bd) + i (bc−ad)
(c2+d2)
Void main ( )
{
complex c1,c2, a, s, m, d ;
cout<<"Enter complex numbers="<<'\n' ;
cn.get complex ( ) ;
cout<<"Enter complex Number = "<<'\n' ;
 
c2. get complex ( ) ;
a. add ( c1, c2) ;
s. subtract (c1,c2) ;
m. multiply (c1,c2) ;
d. divide (c1,c2);
cout<<"Addition \n" ;
   
a.display ( ) ;
cout<<"Subtraction\n" ;
s.display ( ) ;
cout<<"multiplication\n" ;
m.display ( ) ;
cout<<"division\n";
d.display ( );
}

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 ( );

 

}

 


Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet