类继承,复制构造函数和set / get函数

时间:2013-03-13 15:38:39

标签: c++ class inheritance operators copy-constructor

我得到了以下课程:

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 Matrix的类Vector。   我们希望使用Vector类作为Mx1维矩阵的接口   额外的功能。

     

b)为Vector类实现以下构造函数。

     

Vector()
     默认构造函数,应将基础矩阵初始化为无效状态。

     

explicit Vector(unsigned int N)
     应构造基础Mx1矩阵,初始化为零矩阵。 (显式关键字不在教学大纲中,但应在此处使用。)

     

Vector(const Matrix & other);
     来自Matrix的复制构造函数。当且仅当矩阵具有尺寸Nx1时,才应为*指定矩阵,否则结果* this应设置为无效。提示:从Matrix-class重用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?如果我尝试将它从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);
}

非常感谢帮助和提示!

2 个答案:

答案 0 :(得分:1)

接下来是满足一位不称职的教授的可怕的kludgery。 不要在现实世界中这样做。

首先,错误的“复制”构造函数。如果我们不担心尺寸,我们可以做到这一点(不寒而栗):

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

但我们必须首先检查尺寸。我们可以这样做:

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

但是有些笑话忽略了使getColumns() const,所以这会导致编译器错误。我们可以做一些真正激烈的事情, const cast

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

或者只是看起来很糟糕的事情:

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

您需要isValid内容的帮助吗?

答案 1 :(得分:1)

你在集合的正确轨道上得到了。您可以使用语法Class::operator*(args)等成员函数调用运算符。实现向量赋值看起来像这样:

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

您需要将Vector构造函数声明为public。我在想因为你正在使用继承,编译器将为Vector类生成正确的复制构造函数和赋值运算符。你应该编写测试来验证这个假设。