can we add these ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Matrix1
{

Protected:
int ** data;
int row,col;
Public:

};
Class Matrix2 : Public Matrix
{
Public:
Matrix2()
{

}
Matrix2 (int a, int b):Matrix1(a,b)
{

}
void resize()
{

//?????

}

};

// can we do this in our driver/main file(function) i.e.
Matrix1 m1(3,4);
Matrix2 m2(3,4);
Matrix2 m=m1+m2;
in the above statement on the right hand side i.e. m1+m2 ... m1 is of Matrix1 type due to which it will call its +operator and add object m2 in it and return an object of type Matrix1 ... and on the left side of this statement it is Matrix2 ... is this assignment possible , if no then how can i do this ! i am confused !
Yes, operator+ can be overloaded for user-defined types.

What does matrix addition do, logically?
It will Add two matrix objects , simply you can say that add two matrices and return resultant matrix ... ! But my question was is it possible ? because resultant matrix is of Matrix1 type .
Last edited on
Mathematics describes in detail when and how a '+' operation is done for mathematical matrices.

The result of T+U is whatever you want it to be.
If U is-a T, then use T=T+T, because T can be initialized from U.

The last thing to do is to initialize U from T with U::U( const T & ).
Topic archived. No new replies allowed.