home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / m / m003_1 / sb_bc.ddi / BC / CH4 / PROG4-3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-10  |  969 b   |  67 lines

  1. //  PROG4-3.CPP
  2.  
  3. //   C++ Class
  4.  
  5. #include<stdio.h>
  6.  
  7. class complex
  8. { private :
  9.     double real ;
  10.     double imag ;
  11.  
  12.   public :
  13.     void Set(double r, double i) ;
  14.     complex Add(complex c) ;     // this + c
  15.     complex Sub(complex c) ;     // this - c
  16.     void Print(void) ;
  17. } ;
  18.  
  19.  
  20. void complex::Set(double r, double i)
  21. {
  22.   real = r ;
  23.   imag = i ;
  24. }
  25.  
  26.  
  27. complex complex::Add(complex c)
  28. { complex temp ;
  29.  
  30.   temp.real = real + c.real ;
  31.   temp.imag = imag + c.imag ;
  32.  
  33.   return temp ;
  34. }
  35.  
  36.  
  37. complex complex::Sub(complex c)
  38. { complex temp ;
  39.  
  40.   temp.real = real - c.real ;
  41.   temp.imag = imag - c.imag ;
  42.  
  43.   return temp ;
  44. }
  45.  
  46.  
  47. void complex::Print(void)
  48. {
  49.   printf("%g + %g i", real, imag) ;
  50. }
  51.  
  52.  
  53. int main()
  54. { complex a,b,c,d ;
  55.  
  56.   a.Set(1.0, 2.0) ;
  57.   b.Set(3.0, 4.0) ;
  58.  
  59.   c = a.Add(b) ;
  60.   printf("c = ") ; c.Print() ; printf("\n") ;
  61.  
  62.   d = a.Add(b.Sub(c)) ;
  63.   printf("d = ") ; d.Print() ; printf("\n") ;
  64.  
  65.   return 0 ;
  66. }
  67.