2013-03-13 42 views
0

我得到了下面的類:類的繼承,拷貝構造函數和設置/獲取功能

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); 
} 

幫助和提示,非常感謝!

+2

如果這是你的教授給你的練習,你應該告訴他學習正確的C++,並在互聯網上以親切的問候。這是一個可怕的運動。 – 2013-03-13 15:52:28

+0

怎麼回事?是的,這是一所大學的官方演習。 – Ole1991 2013-03-13 15:54:26

+0

首先,複製構造函數只複製同一類的另一個實例,任何東西都不是複製ctor。其次,無論如何,將一個類初始化爲無效狀態到底是什麼?創建一個可以實現Matrix已有功能的矢量類的要點是什麼? – 2013-03-13 15:58:09

回答

1

接下來的事情就是滿足一位不稱職的教授的醜惡詭計。 不要在現實世界中做到這一點。

首先,錯誤的「複製」構造函數。如果我們不擔心尺寸,我們可以這樣做(不寒而慄):

Vector(const Matrix & other) 
{ 
    *this = other; 
} 

但是,我們必須先檢查尺寸。我們可以這樣來做:

Vector(const Matrix & other) 
{ 
    if(other.getColumns()==1) 
     *this = other; 
} 

但有些chucklehead忽視,使getColumns()常量,所以這會導致編譯錯誤。我們可以做一些真正的激烈,常量投

Vector(const Matrix & other) 
{ 
    Matrix *p = const_cast<Matrix *>(&other); 
    if(p->getColumns()==1) 
     *this = other; 
} 

或者只是一些facepalmingly可怕:

Vector(const Matrix & other) 
{ 
    Matrix M(other); // notice that this is not const 
    if(M.getColumns()==1) 
     *this = other; 
} 

你需要的東西isValid幫助?

+0

謝謝,我會給你一個去。但是它在哪裏說getColumns()是const?另外,我如何重複使用Matrix類中的operator =? – Ole1991 2013-03-13 16:41:05

+0

@ Ole1991:它應該在'Matrix.h'中說'getColumns()'是常量,但它不是。而這段代碼使用'operator =',因此:'* this = other;'。現在我看看它,'isValid'這個東西並不是微不足道的。你確定你不能修改'Matrix'類嗎? – Beta 2013-03-13 16:43:53

+0

是的,我可以修改它。 – Ole1991 2013-03-13 16:45:44

1

你正處於正確的軌道上,並獲取。您可以使用語法Class::operator*(args)的成員函數調用運算符。實現向量分配將如下所示:

Vector & Vector::operator=(const Vector &v){ 
    Matrix::operator=(v); 
    return *this; 
} 

您會希望將您的Vector構造函數聲明爲public。我在想,因爲你正在使用繼承,編譯器將爲Vector類生成正確的拷貝構造函數和賦值操作符。你應該寫測試來驗證這個假設。

+0

謝謝!我試試這個:) – Ole1991 2013-03-13 16:42:58

+0

「你會希望你的Vector構造函數被公開。」現在才注意到這一點。我打算讓他們公開,當然:) – Ole1991 2013-03-13 17:25:53