Search code examples
c++classinherited

Inherited Class Problems


I have the next code and i dont know how to solve the mult function. the compiler send me a message cannot declare parameter 'a' to be of type 'Matriz'

I'm supposed to use exactly the code that is in the main

The message is cannot declare parameter 'a' to be of type 'Matriz'

enter code here
#include <iostream>
#include <iomanip>
using namespace std;
// Base
class IMatriz {
   int **m;
   int numRows;
   int numColumns;
   public:
      IMatriz(){
         numRows = 0;
         numColumns = 0;
         m = NULL;
      }   
      IMatriz(int r, int c) {
         numRows = r;
         numColumns = c;
         m = new int* [numRows];
         for(int i=0; i < numRows; i++)
            m[i] = new int [numColumns];
      }
      virtual void setSize(int r, int c) = 0;
      virtual void setValue(int row, int col, int val) = 0;
      virtual int getValue(int row, int col) = 0;
      virtual int getNumRows() = 0;
      virtual int getNumColumns() = 0;
      virtual void mult(IMatriz a, IMatriz b) = 0;
      virtual void print(void) = 0;
};
// Inherited
class Matriz : public IMatriz {
   protected:
      int **m;
      int numRows;
      int numColumns;   
   public:
      Matriz()
     :  IMatriz()
     {
     }

  Matriz(int r, int c)
     :  IMatriz(r, c)
     {
        numRows = r;
        numColumns = c;
        m = new int* [numRows];

        for(int i=0; i < numRows; i++)
           m[i] = new int [numColumns];
     }

  void setSize(int r, int c);
  void setValue(int row, int col, int val);
  int getValue(int row, int col);
  int getNumRows();
  int getNumColumns();
  void mult(Matriz a, Matriz b);
  void print();
};
// Functions
void Matriz::setSize(int r, int c) {  
   numRows = r;
   numColumns = c;
}
void Matriz::setValue(int row, int col, int val) {    
   m[row][col] = val;
}
int Matriz::getValue(int row, int col) {
   return m[row][col];
}
int Matriz::getNumRows() {
   return numRows;
}
int Matriz::getNumColumns() {
   return numColumns;
}
**void Matriz::mult(Matriz a, Matriz b) {**
//   m.setSize( a.getNumRows(), b.getNumColumns() );
//   for (int rows = 0; rows < numRows; rows ++)
//      for (int cols = 0; cols < numColumns; cols ++)
//         m[rows][cols] = 0;
//   for (int rows = 0; rows < a.getNumRows(); rows ++)
//      for (int cols = 0; cols < b.getNumColumns(); cols ++)
//         for (int auxl = 0; auxl < a.getNumColumns(); auxl ++)
//            m[rows][cols] += (a[rows][auxl] * b[auxl][cols]);
   return;
}
void Matriz::print() {
   for (int rows  = 0; rows < numRows; rows ++)
   {
      for (int cols = 0; cols < numColumns; cols ++)
         cout << m[rows][cols] << " ";

      cout << endl;
   }
}
// Principal
int main() {
   Matriz m;
   Matriz a(3, 2);
   Matriz b(2, 3);
   a.setValue(0,0,7);
   a.setValue(0, 0, 7);
   a.setValue(1, 0, 1);
   a.setValue(2, 0, 8);
   a.setValue(0, 1, 2);
   a.setValue(1, 1, 5);
   a.setValue(2, 1, 6);
   b.setValue(0, 0, 2);
   b.setValue(1, 0, 3);
   b.setValue(0, 1, 5);
   b.setValue(1, 1, 4);
   b.setValue(0, 2, 8);
   b.setValue(1, 2, 9);
   a.print();
   b.print();
//   m.mult(a,b);
//   m.print();
   return 0;
}

Solution

  • Made some modification in your code. Please find fixed code below -

    #include <iostream>
    #include <iomanip>
    using namespace std;
    // Base
    
    class IMatriz;
    
    class IMatriz {
        int **m;
        int numRows;
        int numColumns;
    public:
        IMatriz(){
            numRows = 0;
            numColumns = 0;
            m = NULL;
        }
        IMatriz(int r, int c) {
            numRows = r;
            numColumns = c;
            m = new int* [numRows];
            for(int i=0; i < numRows; i++)
                m[i] = new int [numColumns];
        }
        virtual void setSize(int r, int c) = 0;
        virtual void setValue(int row, int col, int val) = 0;
        virtual int getValue(int row, int col) = 0;
        virtual int getNumRows() = 0;
        virtual int getNumColumns() = 0;
        virtual void mult(IMatriz &a, IMatriz &b) = 0;
        virtual void print(void) = 0;
    };
    // Inherited
    class Matriz : public IMatriz {
    protected:
        int **m;
        int numRows;
        int numColumns;
    public:
        Matriz()
        :  IMatriz()
        {
        }
    
        Matriz(int r, int c)
        :  IMatriz(r, c)
        {
            numRows = r;
            numColumns = c;
            m = new int* [numRows];
    
            for(int i=0; i < numRows; i++)
                m[i] = new int [numColumns];
        }
    
        void setSize(int r, int c);
        void setValue(int row, int col, int val);
        int getValue(int row, int col);
        int getNumRows();
        int getNumColumns();
        void mult(IMatriz &a, IMatriz &b);
        void print();
    };
    // Functions
    void Matriz::setSize(int r, int c) {
        numRows = r;
        numColumns = c;
    }
    void Matriz::setValue(int row, int col, int val) {
        m[row][col] = val;
    }
    int Matriz::getValue(int row, int col) {
        return m[row][col];
    }
    int Matriz::getNumRows() {
        return numRows;
    }
    int Matriz::getNumColumns() {
        return numColumns;
    }
    void Matriz::mult(IMatriz &a, IMatriz &b) {
        //   m.setSize( a.getNumRows(), b.getNumColumns() );
        //   for (int rows = 0; rows < numRows; rows ++)
        //      for (int cols = 0; cols < numColumns; cols ++)
        //         m[rows][cols] = 0;
        //   for (int rows = 0; rows < a.getNumRows(); rows ++)
        //      for (int cols = 0; cols < b.getNumColumns(); cols ++)
        //         for (int auxl = 0; auxl < a.getNumColumns(); auxl ++)
        //            m[rows][cols] += (a[rows][auxl] * b[auxl][cols]);
        return;
    }
    void Matriz::print() {
        for (int rows  = 0; rows < numRows; rows ++)
        {
            for (int cols = 0; cols < numColumns; cols ++)
                cout << m[rows][cols] << " ";
    
            cout << endl;
        }
    }
    // Principal
    int main(int argc, const char * argv[]) {
        Matriz m;
        Matriz a(3, 2);
        Matriz b(2, 3);
        a.setValue(0,0,7);
        a.setValue(0, 0, 7);
        a.setValue(1, 0, 1);
        a.setValue(2, 0, 8);
        a.setValue(0, 1, 2);
        a.setValue(1, 1, 5);
        a.setValue(2, 1, 6);
        b.setValue(0, 0, 2);
        b.setValue(1, 0, 3);
        b.setValue(0, 1, 5);
        b.setValue(1, 1, 4);
        b.setValue(0, 2, 8);
        b.setValue(1, 2, 9);
        a.print();
        b.print();
        //   m.mult(a,b);
        //   m.print();
        return 0;
    }
    

    This is the output i get on xcode console -

    7 2 
    1 5 
    8 6 
    2 5 8 
    3 4 9