Hellko

complex

Mar 13th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. class complex {
  5. private:
  6.     double r,i;
  7. public:
  8.     complex(void);
  9.     complex(double,double);
  10.     double abs(void);
  11.     void show(void);
  12.     complex co(void);
  13.     //friend complex operator+(complex,complex);
  14.     complex operator+(complex);     //c=a+b
  15.     complex operator++(void); //prefix  ++a
  16.     complex operator++(int); //postfix  a++
  17. };
  18. complex complex::operator +(complex b) {
  19.     return complex(r+b.r,i+b.i);
  20. }
  21.  
  22. /*complex operator+(complex a,complex b) {
  23.     return complex(a.r+b.r,a.i+b.i);
  24. }*/
  25.  
  26. complex complex::co(void) {
  27.     return complex(r,-i);
  28. }
  29.  
  30. complex::complex(void) {
  31.     r=0;
  32.     i=0;
  33. }
  34. complex::complex(double a,double b) {
  35.     r=a;
  36.     i=b;
  37. }
  38. double complex::abs(void) {
  39.     return sqrt(r*r+i*i);
  40. }
  41. void complex::show(void) {
  42.     printf("Re=%lf\nIm=%lf\n\n",r,i);
  43. }
  44. int main() {
  45.     complex a(7,11),b(3,4);
  46.     a=a+b;
  47.     a.show();
  48. }
Advertisement
Add Comment
Please, Sign In to add comment