next up previous contents
Next: Vererbung Up: KlassenKonstrukturen und Destrukturen: Previous: Die Klasse STRING

Die Klasse COMPLEX

Die Klasse COMPLEX stellt eine einfache Definition komplexer Zahlen dar. Sie kann die gleichen Funktionen aus new.h verwenden wie die Klasse STRING. Da die Klassendefinition absolut analog verläuft wie bei STRING, wollen wir uns mit einem Testlauf des Programms begnügen, um die Polymorphie der in new.h definierten Funktionen zu demonstrieren.

cn@cicero:/home/cn/seminar/aktuell/kap2 > ls
class.d	    error.c	    main        new.c     str.h
compl.c	    error.h	    main.c      new.h     str.o
compl.h	    error.o	    main.o      new.o     test1
complex.c   kap2.tar    makefile    str.c     typescript
cn@cicero:/home/cn/seminar/aktuell/kap2 > make complex
gcc -c compl.c
gcc -o complex complex.c compl.o error.o new.o
cn@cicero:/home/cn/seminar/aktuell/kap2 > complex
3.000000 + 8.000000 i
cn@cicero:/home/cn/seminar/aktuell/kap2 >


Der Vollständigkeit halber hier der Code:

/*    (compl.h)       */
      #ifndef COMPL_H
      #define COMPL_H
      
      extern const void * COMPLEX;
      
      #endif
_______________________________________________

/*    (complex.c)       */
      #include "new.h"
      #include "compl.h"
      #include <stdio.h>
      
      
      int main()
      {
       void * c = new(COMPLEX,2,3.0,8.0);
       cout(c);
       printf("\n");
       return 0;
      }
_______________________________________________      

/*    (compl.c)         */      
      #include "new.h"
      #include "error.h"
      #include <stdio.h>
      #include <stdarg.h>
      #include "compl.h"
      
      
      static void * Complex_constructor(void * _self, const int argnr, va_list * _ap);
      static void * Complex_destructor (void * _self);
      static void * Complex_copy       (const void * _self);
      static void   Complex_cout       (const void * _self);
      
      struct Class_Compl {
        size_t size;
        void * (*constructor) (void * self, const int argnr,va_list * app);
        void * (*destructor)  (void * self);
        void * (*copy)        (const void * self);
        void   (*cout)        (const void * self);
      
      };
      
      struct Complex {
        const void * class;
        double real;
        double imag;
      };
      
      static const struct Class_Compl _Complex = {
        sizeof(struct Complex),
        Complex_constructor,
        Complex_destructor,
        Complex_copy,
        Complex_cout
      };
      
      
      const void * COMPLEX = & _Complex;
      
      
      static void * Complex_constructor(void * _self,const int argnr,va_list * app)
      {
        struct Complex * self = _self;
      
        if (self==0)
          fatalerror(NULLPTR,1);
        switch (argnr)
        {
          case 0:  self->real=0;
      	     self->imag=0;
      	     break;
          case 1:  self->imag=0; 
      	     self->real=va_arg(*app,double);
      	     break;
          case 2:  self->real=va_arg(*app,double);
      	     self->imag=va_arg(*app,double);
      	     break;
          default:
      	     fatalerror("wrong number of arguments - constructor COMPLEX\n",1);
         }
         return self;
      }
      
      static void * Complex_destructor(void * _self)
      {
        struct Complex * self = _self;
        if (self==0)
          fatalerror(NULLPTR,1);
        return self;
      }
      
      static void * Complex_copy(const void * _self)
      {
        const struct Complex * self = _self;
        if (self==0)
          fatalerror(NULLPTR,1);
        if (self->class != COMPLEX)
          fatalerror(TYPE,1);
        return new( COMPLEX, 2, self->real, self->imag);
      }
      
      
      static void Complex_cout(const void * _self)
      {
        const struct Complex * self = _self;
        if (self==0)
          fatalerror(NULLPTR,1);
        if (self->class != COMPLEX)
          fatalerror(TYPE,1);
        fprintf(stdout, "%f + %f i", self->real, self->imag);
      }



Christian Neher