next up previous contents
Next: Working with user defined Up: Working with classes Previous: Working with classes

Using classes in the standard C++ library

As an example of using classes, the program ``complex.C'' in Appendix D illustrates the use of the ``complex<double>'' class. To make sure that you have the C++ compiler working properly, compile and run the program. Note that this program #include's the standard C++ library for complex numbers (so we don't have to program them for ourselves) and, for convenience, makes ``Complex'' a short-hand for ``complex<double>''.

Note that the complex class is used just like any of the familiar variable types int, float, double. The one nice, extra feature is that the standard library includes a handy mechanism for constructing a Complex number whenever we need one (such as 0+2i on the second line of main()).

To practice working with such classes, write a subroutine with the prototype

Complex cmult(Complex a,Complex b);
so that when you uncomment the statement ``z2=cmult(..);'' in ``complex.C'', the program produces the same output as before. In writing this subroutine, you should find that all the familiar rules of working with float and double apply!



Tomas Arias
Sun Apr 15 19:43:40 EDT 2001