如何在不创建多个数组的情况下使用类构造函数创建多个数组
How would I create multiple arrays using a class constructor without creating more than one array in the class?
我正在做一个运算符重载的练习。我创建了一个矩阵类,我应该重载运算符,这样我就可以有效地对矩阵进行算术。
我的方向说,我应该使用具有 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
}
相关文章:
- 在c++中为double类型的数组创建一个unique_ptr
- 如何从预定义的数组创建某些元素数组
- 基于数组的列表 - 动态数组创建时出错
- 从 UTF-8 字节数组创建字符串?
- 是否可以在并行区域中为共享 2D 数组创建选定元素的线程本地副本?(共享,私有,障碍:OPenMP)
- C++ 从名称数组创建对象
- 从数组创建 Mat 时,'cv::Mat' 和 'int' 类型不兼容
- 从 std::数组创建十六进制代表
- 在 C++ 结构内声明的数组 A[1] 创建多个实例
- 为多维数组创建内存空间
- 如何使用任何类型的数组创建模板类并为其设置值?
- 从字符串数组创建一个字符数组
- 是否可以在不为该数组创建变量的情况下将数组作为参数传递到函数中?
- 使用类定义数组创建C++类
- 使用具有唯一随机数的结构数组创建多个对象
- 谷歌测试:无法从字符数组创建字符串
- 从 1D 数组创建 2D 数组
- 无法通过一个单向链表数组创建邻接列表
- 如何使用数组创建标准::函数
- 在C++中,返回从本地字符数组创建的字符串是否会导致内存泄漏或未定义的行为