home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / cplus / 19973 < prev    next >
Encoding:
Text File  |  1993-01-28  |  3.2 KB  |  102 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!news.univie.ac.at!scsing.switch.ch!univ-lyon1.fr!ghost.dsi.unimi.it!rpi!batcomputer!munnari.oz.au!metro!extro.ucc.su.OZ.AU!maxtal
  3. From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
  4. Subject: Re: classes
  5. Message-ID: <1993Jan27.150440.19822@ucc.su.OZ.AU>
  6. Sender: news@ucc.su.OZ.AU
  7. Nntp-Posting-Host: extro.ucc.su.oz.au
  8. Organization: MAXTAL P/L C/- University Computing Centre, Sydney
  9. References: <1jpa7pINNac9@nestroy.wu-wien.ac.at>
  10. Date: Wed, 27 Jan 1993 15:04:40 GMT
  11. Lines: 89
  12.  
  13. In article <1jpa7pINNac9@nestroy.wu-wien.ac.at> h8850513@d21.wu-wien.ac.at (Michael Tesar) writes:
  14. >Hello!
  15. >
  16. >I have a problem with my classes matrix and amatrix.
  17. >Class matrix is for simple 
  18. >matrix operations, eg. multiplication
  19. >In this class I have overloaded the operator*
  20. >
  21. >matrix operator*(matrix& mat);
  22. >
  23. >This class is working well and now I wanted to write a new class amatrix
  24. >with all methodes of the matrix class - but I didnt like to change my 
  25. >matrix class. But in this new class I had to overload the operator* again.
  26. >
  27. >amatrix operator*(const double dvalue);
  28. >
  29. >But now I cannot use the multiplication of two amatrix objects.
  30. >
  31. >The only way to solve this problem - I know - is to write the function
  32. >amatrix operator*(amatrix& mat);
  33. >again, but I think this is not C++ like. 
  34.  
  35.     Why not?
  36. >
  37. >Is there any possibility to avoid this dublication?
  38. >
  39.     If you use a member function, there is no way to
  40. avoid the duplication and there shouldnt be either. After all,
  41. the methods are internally different, you simply must
  42. write a new version.
  43.  
  44.     If you have some methods like 'set(int i, int j, double v)'
  45. in BOTH matrix classes, you can almost solve the problem, however:
  46.  
  47.     class AbstractMatrix {
  48.     public:
  49.         virtual void set(int i, int j, double v)=0;
  50.         virtual double get(int i, int j)const=0;
  51.         virtual int dimen()const=0; // assume square matrix
  52.     };
  53.  
  54.     class matrix : public virtual AbstractMatrix { ... }
  55.     class amatrix : public virtual AbstractMatrix { ... }
  56.  
  57.     AbstractMatrix& operator*=(AbstractMatrix &a,
  58.         const AbstractMatrix& b)
  59.     {
  60.         assert(a.dimen()==b.dimen()); // run-time check
  61.         const int d=a.dimen();
  62.  
  63.         double *temp=new double[d*d];
  64.  
  65.         for ( int i=0; i< d; ++i)
  66.         {
  67.             for( int j=0; j<d; ++j)
  68.             {
  69.                 double sum=0;
  70.                 for (int k=0; k<d; ++k)
  71.                     sum+=a.get(i,k)*b.get(k,j);
  72.                 temp[i*d+j]=sum;
  73.             }
  74.         }
  75.         for(i=0; i<d; ++i)
  76.         for(j=0; i<d; ++j)
  77.             a.set(i,j,temp[i*d+j]);
  78.         delete temp;
  79.         return a;
  80.     }
  81.     // warning untested !!
  82.  
  83. which uses a global function to multiply ANY two matrices 
  84. of equal square dimension together, provided they're
  85. both derived from AbstractMatrix. (Thus they must
  86. have 'get' and 'set' methods, and have 'double' or
  87. equivalent components -- no complex matrices)
  88.  
  89. Nothing stops you defining more efficient versions for special
  90. cases. (Although the way I've set it up the compiler would need
  91. to know the static types of the arguments to call the special
  92. cases).
  93.  
  94. However, note that the operation defined is *= and not *.
  95. Bad luck: you cant have an abstract * operation.
  96.  
  97. -- 
  98. ;----------------------------------------------------------------------
  99.         JOHN (MAX) SKALLER,         maxtal@extro.ucc.su.oz.au
  100.     Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
  101. ;------ SCIENTIFIC AND ENGINEERING SOFTWARE ---ph:  2 799 8223 --------
  102.