Adding two matrix objects and assigning the result to third object using overloaded operators ?
-
here ih the code below i have tried to add two matrix objects and assign it to the third object but it doesn't give expected result and the rather some garbage value.
include
using namespace std;class matrix{
int **p; //pointer to Matrix
int d1, d2;public:
//matrix(){} //default constructor
matrix(int x=20, int y=20); //parameterisedvoid get\_element(); void put\_element(); int checkAdd(matrix &); //to check the feasibility of the operation //Rule of three matrix operator + (matrix &); void operator = (matrix ); ~matrix(){ for (int i = 0; i < d1; i++) delete p\[i\]; delete p; }
};
matrix temp; //global matrix temp
//=============================================
void matrix::operator = (matrix obj){
this->d1 = obj.d1; this->d2 = obj.d2; for(int i=0; ip\[i\]\[j\] = obj.p\[i\]\[j\]; //put\_element(); //return(\*this);
}
matrix matrix::operator+(matrix &obj){
if(checkAdd(obj)==0)
{
//matrix temp;
for(int i=0; ip[i][j] + obj.p[i][j];//temp.put\_element(); return(temp) ; } cout<<"coloumn and rows of both matrix are not equal !"<d1 == obj.d1) && (this->d2 == obj.d2)) return 0; else return 1;
}
void matrix::get_element(){
for(int i=0; i -
here ih the code below i have tried to add two matrix objects and assign it to the third object but it doesn't give expected result and the rather some garbage value.
include
using namespace std;class matrix{
int **p; //pointer to Matrix
int d1, d2;public:
//matrix(){} //default constructor
matrix(int x=20, int y=20); //parameterisedvoid get\_element(); void put\_element(); int checkAdd(matrix &); //to check the feasibility of the operation //Rule of three matrix operator + (matrix &); void operator = (matrix ); ~matrix(){ for (int i = 0; i < d1; i++) delete p\[i\]; delete p; }
};
matrix temp; //global matrix temp
//=============================================
void matrix::operator = (matrix obj){
this->d1 = obj.d1; this->d2 = obj.d2; for(int i=0; ip\[i\]\[j\] = obj.p\[i\]\[j\]; //put\_element(); //return(\*this);
}
matrix matrix::operator+(matrix &obj){
if(checkAdd(obj)==0)
{
//matrix temp;
for(int i=0; ip[i][j] + obj.p[i][j];//temp.put\_element(); return(temp) ; } cout<<"coloumn and rows of both matrix are not equal !"<d1 == obj.d1) && (this->d2 == obj.d2)) return 0; else return 1;
}
void matrix::get_element(){
for(int i=0; i -
I think it may have something to do with your static
temp
object. Try creatingtemp
inside youroperator+
overload.but if i create it inside the operator+ function, i wont be able to assign the value of object temp to object C in main(). As temp scope will only be operator+ function and as soon as the operator+ function is called off, the address that i am passing to the C will no longer point to the values of temp object.
-
but if i create it inside the operator+ function, i wont be able to assign the value of object temp to object C in main(). As temp scope will only be operator+ function and as soon as the operator+ function is called off, the address that i am passing to the C will no longer point to the values of temp object.
-
I have not done operator overloading for a while but that is my recollection of how it should be done. You should be able to find some samples through Google.