如何在不创建多个数组的情况下使用类构造函数创建多个数组

How would I create multiple arrays using a class constructor without creating more than one array in the class?

本文关键字:数组 创建 构造函数 情况下      更新时间:2023-10-16

我正在做一个运算符重载的练习。我创建了一个矩阵类,我应该重载运算符,这样我就可以有效地对矩阵进行算术。

的方向说,我应该使用具有 2 个参数的类构造函数和第三个矩阵数组制作两个矩阵数组,该数组将用于使用默认构造函数(1 个参数)存储算术结果。

由于我将使用这些数组来重载运算符,因此它们将需要成为该类的数据成员(我认为)。但是,我认为类应该尽可能代表现实生活中的事物,因此制作具有多个数组的矩阵类对我来说没有意义(矩阵只是一个矩阵)。

我是否误解了类,或者是否有其他方法可以使用我没有想到的类构造函数制作其他矩阵?谢谢大家,这是有问题的代码。

class matrix
{
    friend ostream& operator << (ostream&, const matrix&); // << overloader 
    private:
    int size // size indicates length of rows and cols, so size 3 means a 3 x 3 matrix
    int array[10][10];
    public:
    matrix(int);
    matrix(int, int);
};
matrix:: matrix (int sizeIn) //default constructor, use to make result matrix
{
    int MAX_SIZE = 10;
    if (0 > sizeIn && sizeIn > 10)
    {
     size = MAX_SIZE;
    }
    else
    {
     size = sizeIn;
    }
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
             array[i][j] = 0;
}
matrix:: matrix (int sizeIn, int rangeIn) //use to make first 2 matrices that will be added
{
    int range;
    int MAX_SIZE = 10;
    int MAX_RANGE = 20;
    if (0 > sizeIn && sizeIn > 10)
    {
     size = MAX_SIZE;
    }
    else
    {
     size = sizeIn;
    }
    if (0 > rangeIn && rangeIn > 20)
    {
      range = MAX_RANGE;
    }
    else
    {
     range = rangeIn;
    }
    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            array[i][j] = (rand() % (2 * range + 1) - range); //random number for each index
}
ostream & operator << (ostream & os, const matrix & arrayPrint) // << overloader
{
    for (int i = 0; i < arrayPrint.size; i++)
    {
        cout << '|';
        for (int j = 0; j < arrayPrint.size; j++)
            {
            os << setw(4) << arrayPrint.array[i][j] << " ";
            }
        os << setw(2) << '|' << endl;
    }
return os;
}

你误解了这个问题。您需要创建一个具有 1 个二维数组的 Matrix 类,然后使用该构造函数创建两个不同的矩阵,然后将它们相加并将结果存储到第三个矩阵中。所以你最终会得到这样的东西

    matrix::matrix(int Size, int Range)
    {
         if(Range < 0 || Range > RANGE_MAX)
             Range = RANGE_MAX;
         if(Size < 0 || Size > SIZE_MAX)
             Size = SIZE_MAX;
        for (int i = 0; i < Size; i++)
            for (int j = 0; j < Size; j++)
               array[i][j] = (rand() % (2 * Range + 1) - Range);
    }

然后您将制作两个矩阵 A 和 B,如下所示

matrix A(4,10); // Create a Matrix A size 4 with random entries in range of 10
matrix B(3,8); // Create a Matrix B with size 3 with random entries in range of 8

然后,您将重载 = 运算符和 + 运算符以执行以下操作(当然,您需要先检查大小是否允许操作)

matrix C = A + B

重载"="运算符

matrix& matrix::operator=(const matrix &rhs)
{
   // if we have A = B then A is me and B is the rhs
   // assuming all members are public you might need to write get/set functions
   size = rhs.size;         // my size equals the right hand side's size
   range = rhs.range;
   for(int i = 0; i < size; i++)
     for(int j = 0; j < size; j++)
        array[i][j] = rhs.array[i][j];
    return *this;   // return me
}