如何让Matrix重载构造函数组合Vector a和Vector b以形成Matrix对象作为外部产品。并对Vector c和Vector d执行相同的操作。问题在于重载构造函数并且能够使用它来创建矩阵。目前它只能在需要使用时才使用Vector a和Vector b。打印成员函数需要从用户输入值打印Matrix。
#include <iostream>
using namespace std;
const int rows=3;
const int columns=3;
const int elements=3;
class Vector{
private:
double data[elements];
public:
Vector();
void read();
double get_element(int);
};
Vector::Vector(){
int i=0;
while(i<elements){data[i++]=0;}
}
void Vector::read(){
int j=0;
cout<<"Enter "<<elements<<" elements of vector "<<endl;
while(j<elements){cin>>data[j++];}
}
double Vector:: get_element(int n){
while(n<elements)
return data[n];
}
Vector a,b,c,d;
class Matrix {
private:
double data [rows*columns];
public:
Matrix(Vector &, Vector &);
void add (const Matrix &);
void mult (double);
double trace();
double norm();
void print ();
};
Matrix::Matrix(Vector &, Vector &){
int d,f;
for (d=0; d<rows; d++){
for (f=0; f<columns;f++){
data[d*f]=a.get_element(d)*b.get_element(f);
}
}
}
Matrix A (a, b);
Matrix B (c, d);
void Matrix::print(){
cout.setf(ios::showpoint|ios::fixed);
cout.precision(3);
for (int i=0; i<rows; i++) {
cout << endl;
for (int j=0; j<columns; j++) {
cout << " " << data[i*j];
}
}
}
答案 0 :(得分:0)
首先,摆脱使用命名空间std。您不想将所有std
导入到您的课程中,只是为了使用cout和cin。
现在让我们了解基础知识。你有一个类,它应该封装/保存自己的数据。这个类可以传递并进行处理。每个类都有自己独特的矢量/矩阵类数据集。
在上面,您的类使用全局变量。这很糟糕,因为每个向量都具有相同的精确数据。它们都共享相同的变量(全局)!
因此我们需要以某种方式让Vector包含自己的数据。我们通过将变量放在vector类本身中来实现。我们将其设为私有,以便无法在课堂外访问。
接下来,我们需要一种初始化data
的方法。我们不能再给它elements
因为元素不再是全局的和不变的。因此,我们现在必须在构造函数中动态分配elements
个双精度数,并在析构函数中删除它。
为了防止现在出现奇怪的行为,我们禁止复制和分配。你应该真正看一下有关类和封装的教程。这个答案将非常不完整,但应该有所帮助并修复一些事情......
如果您100%确定向量必须只有3个元素,仅此而已,那么您可以将double* data
更改为double data[3]
并从构造函数中删除new data[...]
并{来自析构函数的{1}}。
delete[] data
如前所述,如果你100%确定所有矩阵都是3x3且所有向量只有3个元素,那么下面就是这个类的样子:
#include <iostream>
class Vector
{
private:
double* data; //will point to an array of elements.
int elements; //amount of elements this vector has.
Vector(const Vector& other); //copying not allowed.
Vector& operator = (const Vector& other); //copy assignment not allowed.
public:
Vector(int elements); //constructor that tells us how large of an array we need.
~Vector(); //destructor to delete the dynamically allocated array when done.
int size() const; //returns the amount of elements.
double get_element(int n) const;
void set_element(double value, int index);
};
//This is our constructor. It stores the amount of elements we allocated within our class.
//It also initialises data to point to the `new` array.
Vector::Vector(int elements_) : elements(elements_), data(new double[elements_]())
{
}
Vector::~Vector()
{
delete[] data; //before the class gets destroyed, we clean up our dynamically allocated array. ALWAYS!
}
double Vector::get_element(int n) const
{
return data[n];
}
void Vector::set_element(double value, int index)
{
data[index] = value;
}
int Vector::size() const
{
return elements;
}
/** We do the same for the matrix class.**/
class Matrix
{
private:
double* data;
int rows, columns; //instead of elements, we have rows and columns.
Matrix (const Matrix &other); //prevent copying.
Matrix& operator = (const Matrix &other); //prevent assignment.
public:
Matrix(const Vector &a, const Vector &b); //constructor takes TWO vectors.
~Matrix();
void add (const Matrix&);
void mult (double);
double trace();
double norm();
void print ();
};
/** Data is initialized to an array of doubles[rows * columns] **/
/** We also store the amount of rows and columns allocated **/
Matrix::Matrix(const Vector &a, const Vector &b) : data(new double[a.size() * b.size()]), rows(a.size()), columns(b.size())
{
int d, f;
for (d = 0; d < rows; d++)
{
for (f = 0; f < columns; f++)
{
data[d * f] = a.get_element(d) * b.get_element(f);
}
}
}
Matrix::~Matrix()
{
delete[] data; //Before the class is destroyed, we must delete the array.
}
void Matrix::print()
{
std::cout.setf(std::ios::showpoint | std::ios::fixed);
std::cout.precision(3);
for (int i = 0; i < rows; i++)
{
std::cout << std::endl;
for (int j = 0; j < columns; j++)
{
std::cout << " " << data[i * j];
}
}
}
int main()
{
Vector a(3); //Now we can a vector.
Vector b(3); //another vector.
Vector c(2); //notice we can choose how many elements the vector can hold.. 2 elements = a point. Nevertheless..
a.set_element(5.0d, 0);
b.set_element(10.0d, 2);
Matrix m(a, b); //Create a matrix from our two vectors.
m.print(); //Print it..
}