如何使用类构造函数创建多个数组而不在类中创建多个数组?
我正在进行运算符重载的练习。我创建了一个矩阵类,我应该重载运算符,这样我就可以有效地对矩阵进行算术运算。
我的指示说我应该使用具有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你误解了这个问题。您需要创建一个具有 1 个二维数组的 Matrix 类,然后使用构造函数创建两个不同的矩阵,然后将它们相加并将结果存储到第三个矩阵中。所以你最终会得到这样的结果
然后你将创建两个矩阵 A 和 B 如下
你将重载 = 运算符和 + 运算符以让你执行以下操作(当然你需要先检查大小是否允许操作)
重载“=”运算符
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
Then you will make two matrices A and B as follows
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)
To overload the "=" operator