home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- 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
- From: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
- Subject: Re: classes
- Message-ID: <1993Jan27.150440.19822@ucc.su.OZ.AU>
- Sender: news@ucc.su.OZ.AU
- Nntp-Posting-Host: extro.ucc.su.oz.au
- Organization: MAXTAL P/L C/- University Computing Centre, Sydney
- References: <1jpa7pINNac9@nestroy.wu-wien.ac.at>
- Date: Wed, 27 Jan 1993 15:04:40 GMT
- Lines: 89
-
- In article <1jpa7pINNac9@nestroy.wu-wien.ac.at> h8850513@d21.wu-wien.ac.at (Michael Tesar) writes:
- >Hello!
- >
- >I have a problem with my classes matrix and amatrix.
- >Class matrix is for simple
- >matrix operations, eg. multiplication
- >In this class I have overloaded the operator*
- >
- >matrix operator*(matrix& mat);
- >
- >This class is working well and now I wanted to write a new class amatrix
- >with all methodes of the matrix class - but I didnt like to change my
- >matrix class. But in this new class I had to overload the operator* again.
- >
- >amatrix operator*(const double dvalue);
- >
- >But now I cannot use the multiplication of two amatrix objects.
- >
- >The only way to solve this problem - I know - is to write the function
- >amatrix operator*(amatrix& mat);
- >again, but I think this is not C++ like.
-
- Why not?
- >
- >Is there any possibility to avoid this dublication?
- >
- If you use a member function, there is no way to
- avoid the duplication and there shouldnt be either. After all,
- the methods are internally different, you simply must
- write a new version.
-
- If you have some methods like 'set(int i, int j, double v)'
- in BOTH matrix classes, you can almost solve the problem, however:
-
- class AbstractMatrix {
- public:
- virtual void set(int i, int j, double v)=0;
- virtual double get(int i, int j)const=0;
- virtual int dimen()const=0; // assume square matrix
- };
-
- class matrix : public virtual AbstractMatrix { ... }
- class amatrix : public virtual AbstractMatrix { ... }
-
- AbstractMatrix& operator*=(AbstractMatrix &a,
- const AbstractMatrix& b)
- {
- assert(a.dimen()==b.dimen()); // run-time check
- const int d=a.dimen();
-
- double *temp=new double[d*d];
-
- for ( int i=0; i< d; ++i)
- {
- for( int j=0; j<d; ++j)
- {
- double sum=0;
- for (int k=0; k<d; ++k)
- sum+=a.get(i,k)*b.get(k,j);
- temp[i*d+j]=sum;
- }
- }
- for(i=0; i<d; ++i)
- for(j=0; i<d; ++j)
- a.set(i,j,temp[i*d+j]);
- delete temp;
- return a;
- }
- // warning untested !!
-
- which uses a global function to multiply ANY two matrices
- of equal square dimension together, provided they're
- both derived from AbstractMatrix. (Thus they must
- have 'get' and 'set' methods, and have 'double' or
- equivalent components -- no complex matrices)
-
- Nothing stops you defining more efficient versions for special
- cases. (Although the way I've set it up the compiler would need
- to know the static types of the arguments to call the special
- cases).
-
- However, note that the operation defined is *= and not *.
- Bad luck: you cant have an abstract * operation.
-
- --
- ;----------------------------------------------------------------------
- JOHN (MAX) SKALLER, maxtal@extro.ucc.su.oz.au
- Maxtal Pty Ltd, 6 MacKay St ASHFIELD, NSW 2131, AUSTRALIA
- ;------ SCIENTIFIC AND ENGINEERING SOFTWARE ---ph: 2 799 8223 --------
-