【发布时间】:2013-03-01 15:37:40
【问题描述】:
我有以下课程:
class Matrix{
private:
int rows;
int columns;
double* matrix;
public:
Matrix();
explicit Matrix(int N);
Matrix(int M, int N);
void setValue(int M, int N, double value);
double getValue(int M, int N);
bool isValid() const;
int getRows();
int getColumns();
~Matrix();
friend ostream& operator<<(ostream &out, Matrix&matrix1);
Matrix &operator=(const Matrix &m) {
if (rows * columns != m.rows * m.columns){
delete [] this->matrix;
this->matrix = new double[m.rows * m.columns];
}
rows = m.rows;
columns = m.columns;
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
this->matrix[i * columns + j] = m.matrix[i * columns + j];
}
}
return *this;
}
Matrix(const Matrix &rhs);
};
这些功能
#include <iostream>
#include "Matrix.h"
using namespace std;
//OPPGAVE 2
Matrix::Matrix(){
matrix = NULL;
}
Matrix::Matrix(int N){
matrix = new double[N * N];
rows = N;
columns = N;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(i==j)
matrix[i * N + j] = 1;
else
matrix[i * N + j] = 0;
}
}
}
Matrix::Matrix(int M, int N){
matrix = new double[M * N];
rows = M;
columns = N;
for(int i = 0; i < M; i++){
for(int j = 0; j < N; j++)
matrix[i * N + j] = 0;
}
}
Matrix::~Matrix(){
delete [] matrix;
}
void Matrix::setValue(int M, int N, double value){
matrix[M * columns + N] = value;
}
double Matrix::getValue(int M, int N){
return matrix[M * columns + N];
}
bool Matrix::isValid() const{
if(matrix==NULL)
return false;
else
return true;
}
int Matrix::getRows(){
return rows;
}
int Matrix::getColumns(){
return columns;
}
ostream& operator<<(ostream &out, Matrix&matrix1){
if(matrix1.isValid())
for(int i = 0; i < matrix1.getRows(); i++){
for(int j = 0; j < matrix1.getColumns(); j++)
out << matrix1.getValue(i,j) << "\t";
out << endl;
}
else
out << "Matrisen er ikke gyldig." << endl;
return out;
}
Matrix::Matrix(const Matrix &rhs) : rows(rhs.rows),
columns(rhs.columns),
matrix(new double[rows * columns]) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
this->matrix[i * columns + j] = rhs.matrix[i * columns + j];
}
}
}
练习说:
a) 创建一个继承 MxN 矩阵的类 Vector。 我们想使用 Vector 类作为 Mx1 维矩阵的接口,其中包含一些 额外的功能。
b) 为 Vector 类实现以下构造函数。
•
Vector()
默认构造函数,应该将底层矩阵初始化为无效状态。•
explicit Vector(unsigned int N)
应该构造底层的 Mx1 矩阵,初始化为一个零矩阵。 (大纲中没有明确的关键字,但应该在这里使用。)•
Vector(const Matrix & other);
来自 Matrix 的复制构造函数。当且仅当矩阵的维度为 Nx1 时,应将矩阵分配给 *this,否则应将生成的 *this 设置为无效。提示:重用 Matrix 类的 operator=。
这是我目前得到的:
#include "Matrix.h"
class Vector : public Matrix{
public:
Vector();
explicit Vector(int N);
Vector(const Matrix & other);
};
和
using namespace std;
#include <iostream>
#include "Vector.h"
Vector::Vector()
:Matrix(){ }
Vector::Vector(int N)
:Matrix(N,1){ }
我应该如何重用 Matrix 中的 operator=?如果我尝试将它从 Matrix 类复制到 Vector 类,它会说行、列等是不可访问的。我如何访问这些?
是否可以为 Vector 类编写一个与 Matrix 类的拷贝构造函数大致相同的拷贝构造函数?它们都是数组,所以我想应该可以吧?
如果我将 Matrix 与 Vector 相乘,是否会自动使用我为 Matrix 重载的运算符(此处未包含),还是我还需要以某种方式将它们包含在 Vector 类中? (它们是在 Matrix.cpp 文件中的 Matrix 类之外编写的。)
接下来我将为 Vector 类编写 set 和 get 函数。 能不能把这些函数写在这个表格上?:
void Vector::setValue(int i, double value) {
Matrix::setValue(i, 1, value);
}
非常感谢您的帮助和提示!
【问题讨论】:
-
如果这是你的教授给你的练习,你应该告诉他学习正确的 C++,来自 Internet 的亲切问候。这是一个可怕的练习。
-
怎么回事?是的,这是在大学进行的正式练习。
-
首先,拷贝构造函数只是拷贝同一个类的另一个实例,任何东西都不是拷贝ctor。其次,到底是什么将一个类初始化为无效状态?创建一个向量类来做 Matrix 已经做的事情有什么意义?
-
我明白你的意思,但我仍然需要这样做:P
-
这只是
Matrix问题的开始,但根据我的经验,很少有教授会欣赏学生的批评;让我们把这个垃圾堆放在空中。你能告诉我们“this should be set to invalid”是什么意思吗?我用 C++ 编程已经 很长时间 了,但我从来没有听说过。
标签: c++ class inheritance operators copy-constructor