inheritance problem

M

Marc Webel

Hi i have a small(?) problem and i hope you can help me.

i have a basis class "Matrix" that contains a 2d matrix.
you can fill the matrix by a method called "setData(int x,int y)".
two child classes "MatrixTypeA" and "MatrixTypeB´" inherit the matrix
and the filling method. ok.
both childs have different attributes and variables but happen to use
the same matrix type to store the data.

now theres a class "MatrixGenerator" that fills data into the matrix of
both... using a method "fillMatrix()" the problem is:
how can i write a method that accepts "MatrixTypeA" AND "MatrixTypeB"?
both inherit the matrix filling method from "Matrix" thus the filling of
the matrix is always the same(the inherited "setData()" method).

can i somehow write "fillMatrix(Matrix obj)" so it will accept all
classes that are childs of "Matrix"? maybe some sort of cast?

thanks for any insight!

My ideal code should be like this:

MAtrixTypeA a;
MatrixTypeB b;

MatrixGenerator mg;

mg.fillMatrix(a);
mg.fillMatrix(b);
 
V

Victor Bazarov

Marc said:
Hi i have a small(?) problem and i hope you can help me.

i have a basis class "Matrix" that contains a 2d matrix.
you can fill the matrix by a method called "setData(int x,int y)".
two child classes "MatrixTypeA" and "MatrixTypeB?" inherit the matrix
and the filling method. ok.
both childs have different attributes and variables but happen to use
the same matrix type to store the data.

now theres a class "MatrixGenerator" that fills data into the matrix
of both... using a method "fillMatrix()" the problem is:
how can i write a method that accepts "MatrixTypeA" AND "MatrixTypeB"?
both inherit the matrix filling method from "Matrix" thus the filling
of the matrix is always the same(the inherited "setData()" method).

can i somehow write "fillMatrix(Matrix obj)" so it will accept all
classes that are childs of "Matrix"? maybe some sort of cast?

Yes, absolutely. Only don't pass a Matrix object. Pass a reference
to a Matrix object:

said:
thanks for any insight!

My ideal code should be like this:

MAtrixTypeA a;
MatrixTypeB b;

MatrixGenerator mg;

mg.fillMatrix(a);
mg.fillMatrix(b);

Yes. That should work fine.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,217
Latest member
IRMNikole

Latest Threads

Top