home *** CD-ROM | disk | FTP | other *** search
- // PROG4-3.CPP
-
- // C++ Class
-
- #include<stdio.h>
-
- class complex
- { private :
- double real ;
- double imag ;
-
- public :
- void Set(double r, double i) ;
- complex Add(complex c) ; // this + c
- complex Sub(complex c) ; // this - c
- void Print(void) ;
- } ;
-
-
- void complex::Set(double r, double i)
- {
- real = r ;
- imag = i ;
- }
-
-
- complex complex::Add(complex c)
- { complex temp ;
-
- temp.real = real + c.real ;
- temp.imag = imag + c.imag ;
-
- return temp ;
- }
-
-
- complex complex::Sub(complex c)
- { complex temp ;
-
- temp.real = real - c.real ;
- temp.imag = imag - c.imag ;
-
- return temp ;
- }
-
-
- void complex::Print(void)
- {
- printf("%g + %g i", real, imag) ;
- }
-
-
- int main()
- { complex a,b,c,d ;
-
- a.Set(1.0, 2.0) ;
- b.Set(3.0, 4.0) ;
-
- c = a.Add(b) ;
- printf("c = ") ; c.Print() ; printf("\n") ;
-
- d = a.Add(b.Sub(c)) ;
- printf("d = ") ; d.Print() ; printf("\n") ;
-
- return 0 ;
- }
-