Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Adding two matrix objects and assigning the result to third object using overloaded operators ?

Adding two matrix objects and assigning the result to third object using overloaded operators ?

Scheduled Pinned Locked Moved ATL / WTL / STL
question
5 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    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); //parameterised

    void 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

    L 1 Reply Last reply
    0
    • T Tarun Jha

      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); //parameterised

      void 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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I think it may have something to do with your static temp object. Try creating temp inside your operator+ overload.

      T 1 Reply Last reply
      0
      • L Lost User

        I think it may have something to do with your static temp object. Try creating temp inside your operator+ overload.

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • T Tarun Jha

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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.

          T 1 Reply Last reply
          0
          • L Lost User

            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.

            T Offline
            T Offline
            Tarun Jha
            wrote on last edited by
            #5

            ok

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups