Design a class Full that implements interface Matrix: Full should has the following public object functions in addition:
A constructor Full(int rows, int column), which initializes all elements in the matrix to 0's.
A constructor Full(int rows, int column, double values[]), which initializes all elements in the matrix to the given values. Note that the given values are in one-dimensional, you need to fill then into the two-dimensional matrix correctly.
A constructor Full(const Matrix & matrix2).
A destructor.
A print function which prints each row of elements in a single line, with each element preceded with 4 spaces.
HINT
本题涉及 pure virtual function, abstract class 和 interface 的内容。
The function int size(int) function return the 2-dimensional size of the matrix: it returns the size in the first dimension (the number of rows) if the argument is 1, and returns size in the second dimension (the number of column) if the argument is 2.
The class Full that you will define must implement the interface class Matrix, which means that it needs to override the operator = as it is in Matrix, along with all other pure virtual object functions in Matrix.
Don't forget to provide the constructor Full(const Matrix & matrix2) in our class.
EXAMPLE INPUT
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
EXAMPLE OUTPUT
constructor 1
0 0 0
0 0 0
0 0 0
constructor 2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
constructor 3
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
operator =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
主程序 (不能修改)
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class Matrix
{
public:
virtual int size(int dimension) const = 0;
virtual void set(int row, int column, double value) = 0;
virtual double get(int row, int column) const = 0;
virtual void print() const = 0;
virtual Matrix & operator = (const Matrix & matrix2) = 0;
};
#include "source.cpp"
Full readMatrix() {
int rows;
int columns;
double values[1000];
cin >> rows >> columns;
for (int i = 0; i < rows * columns; ++ i) {
cin >> values[i];
}
Full matrix(rows, columns, values);
return matrix;
}
void printMatrix(const Matrix & matrix) {
matrix.print();
}
int main() {
cout << "constructor 1" << endl;
Full full1(3, 3);
Matrix & matrix1 = full1; // down-casting
matrix1.print();
cout << "constructor 2" << endl;
const Matrix & matrix2 = readMatrix();
matrix2.print();
cout << "constructor 3" << endl;
Full full3 = matrix2;
Matrix & matrix3 = full3;
printMatrix(matrix3);
cout << "operator =" << endl;
matrix3.set(1, 1, 10.0);
matrix3 = matrix2;
matrix3.print();
}
我的答案
class Full : public Matrix
{
private:
int rows;
int columns;
double *addr;
void _init(int rows,int columns,double values[]){
this->rows=rows;
this->columns=columns;
this->addr=new double[rows*columns];
for (int i = 0; i < rows*columns ; ++ i) {
this->addr[i]=(values==NULL ? 0: values[i]);
}
}
public:
Full(int rows, int columns){
this->_init(rows,columns,NULL);
}
Full(int rows, int columns, double values[]){
this->_init(rows,columns,values);
}
int size(int dimension)const{
if(dimension==1) return rows;
else return columns;
}
Full(const Matrix & matrix2){
this->rows=matrix2.size(1);
this->columns=matrix2.size(2);
this->addr=new double[rows*columns];
for (int i = 1; i <= rows; ++ i)
for(int j = 1; j <= this->columns; ++ j)
this->addr[(i-1)*this->columns+j-1]=matrix2.get(i,j);
}
Full(const Full & full) {
this->_init(full.rows, full.columns, full.addr);
}
~Full(){
delete [] this->addr;
}
double get(int row, int col) const{
int index = (row-1)* this->columns + col-1;
return this->addr[index];
}
void set(int row, int col,double val){
int index = (row-1)* this->columns + col-1;
this->addr[index] = val;
}
void print() const {
for (int i = 0; i < this->rows; ++ i) {
for (int j = 0; j < this->columns; ++ j) {
cout << " " << this->get(i+1,j+1);
}
cout<<endl;
}
}
Full & operator = (const Matrix & matrix2){
double rows=matrix2.size(1),columns=matrix2.size(2);
for (int i = 1; i <= this->rows; ++ i)
for(int j = 1; j <= this->columns; ++ j)
this->set(i,j,matrix2.get(i,j));
}
};