home *** CD-ROM | disk | FTP | other *** search
- // PROG4-2.CPP
-
- // C++ operator overloading
-
- #include<stdio.h>
-
- struct complex
- { double real ;
- double imag ;
- } ;
-
-
- complex ComplexSet(double r, double i)
- { complex temp ;
-
- temp.real = r ;
- temp.imag = i ;
-
- return temp ;
- }
-
-
- complex operator + (complex c1, complex c2)
- { complex temp ;
-
- temp.real = c1.real + c2.real ;
- temp.imag = c1.imag + c2.imag ;
-
- return temp ;
- }
-
-
- complex operator - (complex c1, complex c2)
- { complex temp ;
-
- temp.real = c1.real - c2.real ;
- temp.imag = c1.imag - c2.imag ;
-
- return temp ;
- }
-
-
- void ComplexPrint(complex c)
- {
- printf("%g + %g i", c.real, c.imag) ;
- }
-
-
- int main()
- { complex a,b,c,d ;
-
- a = ComplexSet(1.0, 2.0) ;
- b = ComplexSet(3.0, 4.0) ;
-
- c = a + b ;
- printf("c = ") ; ComplexPrint(c) ; printf("\n") ;
-
- d = a + b - c ;
- printf("d = ") ; ComplexPrint(d) ; printf("\n") ;
-
- return 0 ;
- }
-