简体   繁体   English

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

[英]Class inheritance, copy constructor and set/get functions

I got the following class: 我得到了以下课程:

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

with these functions 有了这些功能

#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];
            }
        }
    }

The exercise says: 演习说:

a) Create a class Vector that inherits the MxN Matrix. a)创建一个继承MxN Matrix的类Vector。 We want to use the Vector class as an interface to an Mx1 dimensional matrix with some extra functionality. 我们希望使用Vector类作为具有一些额外功能的Mx1维矩阵的接口。

b) Implement the following constructors for the Vector class. b)为Vector类实现以下构造函数。

Vector() Vector()
Default constructor, should initialize the underlying matrix into the invalid state. 默认构造函数,应将基础矩阵初始化为无效状态。

explicit Vector(unsigned int N) explicit Vector(unsigned int N)
Should construct the underlying Mx1 Matrix, initialized as a zero-matrix. 应构造基础Mx1矩阵,初始化为零矩阵。 (The explicit keyword is not in the syllabus, but it should be used here.) (显式关键字不在教学大纲中,但应在此处使用。)

Vector(const Matrix & other); Vector(const Matrix & other);
Copy-constructor from Matrix. 来自Matrix的复制构造函数。 Should assign a matrix to *this if and only if the matrix has dimensions Nx1, otherwise the resulting *this should be set to invalid. 当且仅当矩阵具有尺寸Nx1时,才应为*指定矩阵,否则结果* this应设置为无效。 Hint: Reuse operator= from the Matrix-class. 提示:从Matrix类重用operator =。

This is what I've got so far: 这是我到目前为止所得到的:

#include "Matrix.h"

class Vector : public Matrix{
    public:
        Vector();
        explicit Vector(int N);
        Vector(const Matrix & other);

};

and

using namespace std;
#include <iostream>
#include "Vector.h"

Vector::Vector() 
    :Matrix(){ }

Vector::Vector(int N)
    :Matrix(N,1){ }

How am I supposed to reuse the operator= from Matrix? 我怎么能重用运算符=来自Matrix? If I try to copy it from the Matrix class into the Vector class, it says that rows, columns etc is inaccessible. 如果我尝试将它从Matrix类复制到Vector类,它会说行,列等是不可访问的。 How do I access these? 我如何访问这些?

Is it possible to write a copy constructor for the Vector class more or less the same as the copy constructor for the Matrix class? 是否可以为Vector类编写一个与Matrix类的复制构造函数大致相同的复制构造函数? They are both arrays, so I guess it should work? 它们都是数组,所以我想它应该有效吗?

Will the operators I overloaded for Matrix (not included here) automaticly be used if I multiply a Matrix with a Vector, or do I also need to include these somehow in the Vector class? 如果我将Matrix与Vector相乘,我会自动使用我为Matrix重载的运算符(不包括在这里)吗?或者我是否还需要在Vector类中以某种方式包含它们? (They were written outside the Matrix class in the Matrix.cpp-file.) (它们是在Matrix.cpp文件中的Matrix类之外编写的。)

Next Im going to write set and get functions for the Vector class. 接下来我将为Vector类编写set和get函数。 Is it possible to write these functions on this form?: 是否可以在此表单上编写这些函数?:

void Vector::setValue(int i, double value) {
    Matrix::setValue(i, 1, value);
}

Help and tips are greatly appreciated! 非常感谢帮助和提示!

What follows is hideous kludgery to satisfy an incompetent professor. 接下来是满足一位不称职教授的可怕的kludgery。 Don't do this in the real world. 不要在现实世界中这样做。

First, the misnamed "copy" constructor. 首先,错误的“复制”构造函数。 If we weren't worried about the dimensions, we could do this (shudder): 如果我们不担心尺寸,我们可以做到这一点(不寒而栗):

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

But we must check the dimensions first. 但我们必须首先检查尺寸。 We could do it this way: 我们可以这样做:

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

But some chucklehead neglected to make getColumns() const, so this results in a compiler error. 但有些笑话忽略了使getColumns() const,所以这会导致编译器错误。 We could do something truly drastic, const cast : 我们可以做一些真正激烈的事情, const cast

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

Or just something facepalmingly awful: 或者只是看起来很糟糕的东西:

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

Do you need help with the isValid stuff? 你需要有关isValid帮助吗?

You are on the right track for the sets and gets. 你是在正确的轨道上设置和获取。 You can call operators with member function like syntax Class::operator*(args) . 您可以使用语法Class::operator*(args)等成员函数调用运算Class::operator*(args) Implementing the vector assignment would look something like this: 实现向量赋值看起来像这样:

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

You will want your Vector constructors to be declared public. 您需要将Vector构造函数声明为public。 I am thinking because you are using inheritance the compiler will generate correct copy constructors and assignment operators for the Vector class. 我在想因为你正在使用继承,编译器将为Vector类生成正确的复制构造函数和赋值运算符。 You should write tests to verify this assumption. 你应该编写测试来验证这个假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM