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. C / C++ / MFC
  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 C / C++ / MFC
question
9 Posts 3 Posters 0 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

    V L 2 Replies 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

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      What result did you expect and what did you get?

      T 1 Reply Last reply
      0
      • V Victor Nijegorodov

        What result did you expect and what did you get?

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

        Quote:

        So basically i want to add 2 matrix object and assign the result to the third non-initiated matrix object, without changing the values of first 2 original matrix.

        this is the result i am getting.

        Quote:

        Enter matrix elements row by row m[0][0] = 1 m[0][1] = 2 m[1][0] = 3 m[1][1] = 4 m[2][0] = 5 m[2][1] = 6 1 2 3 4 5 6 m[0][0] = 7 m[0][1] = 8 m[1][0] = 9 m[1][1] = 10 m[2][0] = 11 m[2][1] = 12 7 8 9 10 11 12 8 10 47120720 0 0 0 0 0 0 0 50331651 41272 47127056 0 47120720 0 0 2396 136 0 12 14 47120720 0 1550742898 1148219457 1549890657 1633906508 1700027500 1426092141 1146242387 1229016399 1162100046 1330924371 1429482832 844253517 1398079566 1329877573 1313423693 1095717471 16 18 47120720 0 1162690894 1918981181 1426091637 1347568979 1229344594 1128088908 1934974010 1551069797 1970430292 1398145134 1127232561 1414417743 1397509967 1547322173 1735357008 544039282 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 47120720 0 47127424

        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
          #4

          You already posted this exact question in the ATL/STL forum, and I gave you a suggestion. If you google for "operator overload C++" you will find plenty of samples.

          T 1 Reply Last reply
          0
          • L Lost User

            You already posted this exact question in the ATL/STL forum, and I gave you a suggestion. If you google for "operator overload C++" you will find plenty of samples.

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

            i was told that these type of questions are to be posted here. and i did the program..

            #include
            using namespace std;

            class matrix{
            int **p; //pointer to Matrix
            int d1, d2;
            int id=0; //identif which operator is used

            public:
            //matrix(){} //default constructor
            matrix(int x=20, int y=20); //parameterised

            //to pass private data-members to other objects
            int getD\_1(){return (d1);}
            int getD\_2(){return (d2);}
            //int getId(const int x){id =x ;}
            //void showId(){cout<<"ID :\\t"<d1 = obj.d1;
            this->d2 = obj.d2;
            this->id = obj.id;
            for(int i=0; ip\[i\]\[j\] = obj.p\[i\]\[j\];
            

            }

            //defining overloaded addition operator
            matrix operator+(matrix &a, matrix &b){
            matrix temp;
            temp.d1 = a.d1;
            temp.d2 = a.d2;
            temp.id = 1;

            if(checkAdd(a, b) == 0){                //checks the viability if it's feasible or not.
                for(int i=0; i
            
            L 1 Reply Last reply
            0
            • T Tarun Jha

              i was told that these type of questions are to be posted here. and i did the program..

              #include
              using namespace std;

              class matrix{
              int **p; //pointer to Matrix
              int d1, d2;
              int id=0; //identif which operator is used

              public:
              //matrix(){} //default constructor
              matrix(int x=20, int y=20); //parameterised

              //to pass private data-members to other objects
              int getD\_1(){return (d1);}
              int getD\_2(){return (d2);}
              //int getId(const int x){id =x ;}
              //void showId(){cout<<"ID :\\t"<d1 = obj.d1;
              this->d2 = obj.d2;
              this->id = obj.id;
              for(int i=0; ip\[i\]\[j\] = obj.p\[i\]\[j\];
              

              }

              //defining overloaded addition operator
              matrix operator+(matrix &a, matrix &b){
              matrix temp;
              temp.d1 = a.d1;
              temp.d2 = a.d2;
              temp.id = 1;

              if(checkAdd(a, b) == 0){                //checks the viability if it's feasible or not.
                  for(int i=0; i
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Forget about matrix calculations. You need to go and study operator overloading and get that working on a basic class first. Once you understand how to do it correctly so you get no errors, then you can move on to the matrix functions.

              T 1 Reply Last reply
              0
              • L Lost User

                Forget about matrix calculations. You need to go and study operator overloading and get that working on a basic class first. Once you understand how to do it correctly so you get no errors, then you can move on to the matrix functions.

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

                is there anything wrong with the program i have posted before ?

                L 1 Reply Last reply
                0
                • T Tarun Jha

                  is there anything wrong with the program i have posted before ?

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

                  I don't know; you have to do your own testing. But I find one of the easiest way to debug issues is to create a small test program that just focused on the area that appears not to work. So a small program with a very basic class, that just tests the operator+ overload, would be much easier to understand.

                  T 1 Reply Last reply
                  0
                  • L Lost User

                    I don't know; you have to do your own testing. But I find one of the easiest way to debug issues is to create a small test program that just focused on the area that appears not to work. So a small program with a very basic class, that just tests the operator+ overload, would be much easier to understand.

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

                    ok... thanks for the advice

                    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