16. 实例化一个对象

实例化一个对象

现在,是时候在程序中使用你的矩阵类了!C++ 中实例化对象的语法如下所示:

Classname objectname(inputs for initializing an object of Classname);

然后,你就可以访问任何公共变量,如:

objectname.variablename

你还可以访问公共函数:

objectname.methodname(inputs)

请记住,你的程序无法访问任何私有变量或函数。这就是你需要为你的私有变量编写公共的 get 和 set 函数的原因。

Gaussian.cpp 例子

在开始使用矩阵类之前,先看看 Gaussian.cpp 中 main.cpp 的一个例子:

# include <iostream>
# include "gaussian.h"

int main ()
{

    Gaussian mygaussian(30.0,20.0);
    Gaussian othergaussian(10.0,30.0);

    std::cout << "average " << mygaussian.getMu() << std::endl;
    std::cout << "evaluation " << mygaussian.evaluate(15.0) << std::endl;

    std::cout << "mul results sigma " << 
               mygaussian.mul(othergaussian).getSigma2() << std::endl;
    std::cout << "mul results average " << 
               mygaussian.mul(othergaussian).getMu() << std::endl;

    std::cout << "add results sigma " << 
               mygaussian.add(othergaussian).getSigma2() << std::endl;
    std::cout << "add results average " << 
               mygaussian.add(othergaussian).getMu() << std::endl;

    return 0;
}

现在轮到你编写矩阵对象了。下面提供了一些辅助代码,有一些 TODO 部分需要你完成。

Start Quiz:

#include <iostream>
#include <vector>
#include "matrix.h"

int main () {
    
    // assign a 7x5 matrix to the variable initial_grid
    // all values in the matrix are 0.4
	std::vector <std:: vector <float> > 
	    initial_grid (7, std::vector <float>(5, 0.4));

    // TODO: Use the initial grid variable to instantiate a matrix object
    // The matrix object should be called matrixa
    
    // TODO: Use the matrix_print() method to print out matrixa
    
    // TODO: Print out the number of rows in matrixa. You will need
    // to use the getRows() function and std::cout
    
    // TODO: Print out the number of columns in matrixa 
    
    // TODO: Take the transpose of matrixa and store the results in
    // a variable called transposea

    // TODO: Print out transposea

    // Now you will use another 7x5 matrix called matrixb to 
    // give the results of the matrix_addition function
    
    // 7x5 2-dimensional vector with values 0.2
	std::vector <std:: vector <float> > 
	    second_grid (7, std::vector <float>(5, 0.2));
    
    // TODO: Instantiate an object called matrixb. Use the second_grid
    // variable as the input to initialize matrixb
    
    // TOOD: Add matrixa and matrixb. Store the results in a new matrix
    // variable called matrixsum
    
    // TODO: Print out the matrix contained in the matrixsum variable

    return 0;
}
// Nothing to do here

#include "matrix.h"

Matrix::Matrix() {
    std::vector <std:: vector <float> > initial_grid (10, std::vector <float>(5, 0.5));
    grid = initial_grid;
    rows = initial_grid.size();
    cols = initial_grid[0].size();

}

Matrix::Matrix(std::vector <std:: vector <float> > initial_grid) {
    grid = initial_grid;
    rows = initial_grid.size();
    cols = initial_grid[0].size();
}

void Matrix::setGrid(std::vector< std::vector<float> > new_grid) {
    grid = new_grid;
    rows = new_grid.size();
    cols = new_grid[0].size();

}

std::vector< std::vector<float> > Matrix::getGrid() {
    return grid;
}

std::vector<float>::size_type Matrix::getRows() {
    return rows;
}

std::vector<float>::size_type Matrix::getCols() {
    return cols;
}

Matrix Matrix::matrix_transpose() {
    std::vector< std::vector<float> > new_grid;
    std::vector<float> row;

    for (int i = 0; i < cols; i++) {
        row.clear();

        for (int j = 0; j < rows; j++) {
            row.push_back(grid[j][i]); 
        }
        new_grid.push_back(row);
    }

    return Matrix(new_grid);
}

Matrix Matrix::matrix_addition(Matrix other) {

    if ((rows != other.getRows()) || (cols != other.getCols())) {
        throw std::invalid_argument( "matrices are not the same size" );
    }

    std::vector< std::vector<float> > othergrid = other.getGrid();

    std::vector< std::vector<float> > result;

    std::vector<float> new_row;

    for (int i = 0; i < rows; i++) {
        new_row.clear();
        for (int j = 0; j < cols; j++) {
            new_row.push_back(grid[i][j] + othergrid[i][j]);
        }
        result.push_back(new_row);
    }

    return Matrix(result);
}

void Matrix::matrix_print() {

    std::cout << std::endl;

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            std::cout << grid[i][j] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}
// Nothing to do here
#ifndef MATRIX_H
#define MATRIX_H

#include <vector>
#include <iostream>
#include <stdexcept>

#include <vector>

class Matrix
{
    private:

        std::vector< std::vector<float> > grid;
        std::vector<float>::size_type rows;
        std::vector<float>::size_type cols;

    public:

        // constructor functions
        Matrix ();
        Matrix (std::vector< std::vector<float> >);

        // set functions
        void setGrid(std::vector< std::vector<float> >);

        // get functions
        std::vector< std::vector<float> > getGrid();
        std::vector<float>::size_type getRows();
        std::vector<float>::size_type getCols();

        // matrix functions
        Matrix matrix_transpose();
        Matrix matrix_addition(Matrix);

        // matrix printing
        void matrix_print();

};

#endif /* MATRIX_H */

main.cpp 参考答案

# include <iostream>
# include <vector>
# include "matrix.h"

int main () {

    // 给变量 initial_grid 分配一个 7x5 矩阵
    // 矩阵中的所有值都是 0.4
    std::vector <std:: vector <float> > 
        initial_grid (7, std::vector <float>(5, 0.4));

    // TODO:使用初始 grid 变量来实例化一个矩阵对象
    // 矩阵对象应该写作 matrixa
    Matrix matrixa(initial_grid);

    // TODO:使用 matrix_print() 方法打印出 matrixa
    matrixa.matrix_print();

    // TODO:打印出 matrixa 中的行数。你需要
    //使用 getRows() 函数和 std::cout
    std::cout << matrixa.getRows();

    // TODO:打印出 matrixa 中的列数 
    std::cout << matrixa.getCols();

    // TODO:取矩阵的转置并把结果存储在
    //一个名叫 transposea 的变量里
    Matrix transposea = matrixa.matrix_transpose();

    // TODO:打印出 transposea
    transposea.matrix_print();

    // 现在你需要使用另一个名为 matrixb 的 7x5 矩阵,来 
    //给出 matrix_addition 函数的结果

    // 7x5 二维矩阵,所有值均为 0.2
    std::vector <std:: vector <float> > 
        second_grid (7, std::vector <float>(5, 0.2));

    // TODO:实例化一个叫做 matrixb 的对象使用 second_grid
    // 变量作为初始化 matrixb 的输入
    Matrix matrixb(second_grid);

    // TOOD:matrixa 和 matrixb 相加。将结果存储在一个新的矩阵中
    //变量名为 matrixsum
    Matrix matrixsum(matrixa.matrix_addition(matrixb));

    // TODO:打印出 matrixsum 变量中包含的矩阵
    matrixsum.matrix_print();

    return 0;
}