我得到了下面的類:類的繼承,拷貝構造函數和設置/獲取功能
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];
}
}
}
演習說:
一)創建繼承MxN矩陣的類Vector。 我們希望使用Vector類作爲Mx1維矩陣的接口,並具有一些 額外的功能。
b)爲Vector類實現以下構造函數。
•
Vector()
默認構造函數應該將基礎矩陣初始化爲無效狀態。•
explicit Vector(unsigned int N)
應構造基礎Mx1矩陣,初始化爲零矩陣。 (顯式關鍵字不在教學大綱中,但應在此使用。)•
Vector(const Matrix & other);
Matrix的複製構造函數。當且僅當矩陣的維數爲Nx1時,應該爲*賦這個矩陣,否則產生的*應該被設置爲無效。提示:重新使用矩陣類中的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類複製到Vector類中,它說行,列等是不可訪問的。我如何訪問這些?
是否可以爲Vector類編寫複製構造函數,或多或少與Matrix類的複製構造函數相同?他們都是陣列,所以我想它應該工作?
如果我將Matrix與Vector相乘,或者是否還需要在Vector類中包含這些函數,那麼是否會自動使用Matrix重載的算子(此處未包含)? (它們是在Matrix.cpp文件中的Matrix類以外編寫的。)
接下來,我將爲Vector類編寫set和獲取函數。 是否有可能寫這個表格上這些功能?:
void Vector::setValue(int i, double value) {
Matrix::setValue(i, 1, value);
}
幫助和提示,非常感謝!
如果這是你的教授給你的練習,你應該告訴他學習正確的C++,並在互聯網上以親切的問候。這是一個可怕的運動。 – 2013-03-13 15:52:28
怎麼回事?是的,這是一所大學的官方演習。 – Ole1991 2013-03-13 15:54:26
首先,複製構造函數只複製同一類的另一個實例,任何東西都不是複製ctor。其次,無論如何,將一個類初始化爲無效狀態到底是什麼?創建一個可以實現Matrix已有功能的矢量類的要點是什麼? – 2013-03-13 15:58:09