如何使用类构造函数创建多个数组而不在类中创建多个数组?

发布于 2024-11-06 01:28:50 字数 1980 浏览 1 评论 0原文

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

我的指示说我应该使用具有 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;
}

I am working on an exercise in operator overloading. I've created a matrix class and I am supposed to overload operators so I can do arithmetic on matrices efficiently.

My directions say that I am supposed to make two matrix arrays using the class constructor that has 2 parameters and a third matrix array that will be used to store the result of arithmetic using the default constructor (1 parameter).

Since I am going to use these arrays to overload operators they are going to need to be data members of the class (I think). However, I thought that classes were supposed to be as representative of real life things as possible so making a matrix class with multiple arrays doesn't make sense to me (a matrix is only one matrix).

Am I misunderstanding classes or is there a different way to make additional matrices using the class constructor I am not thinking of? Thanks all, here is the code in question.

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哑剧 2024-11-13 01:28:50

你误解了这个问题。您需要创建一个具有 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
}

You are misunderstanding the question. You are required to make a Matrix class that has 1 two dimensional array and then use the constructor to make two different matrices and then add them and store the result into a third one. So you will end up with something like this

    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);
    }

Then you will make two matrices A and B as follows

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

You will then overload the = operator and + operator to let you do the following (of course you need to check if the sizes allow the operation first)

matrix C = A + B

To overload the "=" operator

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文