【问题标题】:Class inheritance, copy constructor and set/get functions类继承、复制构造函数和设置/获取函数
【发布时间】: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 &amp; 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


【解决方案1】:

你在设置和获取的正确轨道上。您可以使用语法Class::operator*(args) 等成员函数调用运算符。实现向量分配看起来像这样:

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

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

【讨论】:

  • 谢谢!我试试看:)
  • "你会希望你的 Vector 构造函数被声明为 public。"现在才注意到这一点。我的意思是把它们公开,当然:)
【解决方案2】:

接下来是为了满足一个不称职的教授而做出的可怕的恶作剧。 不要在现实世界中这样做。

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

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 方面的帮助吗?

【讨论】:

  • 谢谢,我试试看。但是它在哪里说 getColumns() 是 const 呢?另外,我究竟如何重用 Matrix 类中的 operator=?
  • @Ole1991:它应该Matrix.h 中说getColumns() 是const,但它不是。这段代码使用operator=,因此:*this = other;。现在我看了一下,isValid 的东西并不是微不足道的。你确定不能修改Matrix 类吗?
  • 我的复制构造函数、operator= 和 set/get 函数现在可以工作了。谢谢!我想我现在可以自己完成剩下的练习了 :)
猜你喜欢
  • 2012-09-03
  • 1970-01-01
  • 2012-05-26
  • 2014-11-22
  • 2015-04-11
相关资源
最近更新 更多