图像处理C++基础 03 ——类

    xiaoxiao2021-12-14  17

    介绍概念:

    宏。变量revisited(局部变量及作用域,函数的形参及实参,按值传递及按地址传递), 编程style, void *类型,变量命名。程序组织(函数/文件/项目);缺省参数

    选介绍:类(OOP),类的封装性,new/delete操作符,构造,析构 练习: 使用类重复第一课任务,Matrix.文件:main(). 第2套:matrix.h, matrix.cpp

    要求:

    矩阵加法,矩阵乘法。建议在析构里free空间

    (参考:C++高级编程 2版或3版,注意,只要参考第1章。其他的不要看!)

    #include using namespace std; class Matrix { public: void Matrixnew(); /*创建矩阵*/ void Matrixdel(); /*删除矩阵空间*/ friend Matrix operator+(Matrix &, Matrix &); /*矩阵加法*/ friend Matrix operator*(Matrix &, Matrix &); /*矩阵乘法*/ void input(); /*输入矩阵元素*/ void display(); /*输出矩阵元素*/ int rows = 0; /*矩阵的行、列数*/ int columns = 0; private: int **mat = NULL; /*二维动态矩阵*/ }; #include using namespace std; class Matrix { public: void Matrixnew(); /*创建矩阵*/ void Matrixdel(); /*删除矩阵空间*/ friend Matrix operator+(Matrix &, Matrix &); /*矩阵加法*/ friend Matrix operator*(Matrix &, Matrix &); /*矩阵乘法*/ void input(); /*输入矩阵元素*/ void display(); /*输出矩阵元素*/ int rows = 0; /*矩阵的行、列数*/ int columns = 0; private: int **mat = NULL; /*二维动态矩阵*/ }; void Matrix::Matrixnew() { mat = new int *[rows]; for (int i = 0; i < rows; i++) mat[i] = new int[columns]; for (int i = 0; i < rows; i++) for (int j = 0; j < columns; j++) mat[i][j] = 0; } void Matrix::Matrixdel() { for (int i = 0; i < rows; i++) delete mat[i]; delete mat; } Matrix operator+(Matrix &a, Matrix &b) { Matrix c; c.rows = a.rows; c.columns = b.columns; c.Matrixnew(); for (int i = 0; i < a.rows; i++) for (int j = 0; j < a.columns; j++) c.mat[i][j] = a.mat[i][j] + b.mat[i][j]; return c; } Matrix operator*(Matrix &a, Matrix &b) { Matrix c; c.rows = a.rows; c.columns = b.columns; c.Matrixnew(); for (int i = 0; i < a.rows; i++) for (int j = 0; j < b.columns; j++) for (int k = 0; k < a.columns; k++) c.mat[i][j] += a.mat[i][k] * b.mat[k][j]; return c; } void Matrix::input() { cout << "Please enter the elements of the matrix:" << endl; for (int i = 0; i < rows; i++) for (int j = 0; j < columns; j++) cin >> mat[i][j]; } void Matrix::display() { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) cout << mat[i][j] << '\t'; cout << endl; } cout << endl; } int main(void) { Matrix a, b, c; int i, j, k, flag = 0; cout << "Please enter the rows and the columns of the first matrix:"; cin >> i >> j; cout << "Please enter the columns of the second matrix:"; cin >> k; cout << "Which result do you want to get?" << endl; cout << "1.a+b=" << endl << "2.a*b" << endl; cin >> flag; a.rows = i; a.columns = j; b.rows = j; b.columns = k; c.rows = i; c.columns = k; a.Matrixnew(); b.Matrixnew(); c.Matrixnew(); a.input(); b.input(); if (flag == 1) { c = a + b; cout << "The result of Matrix a add Matrix b is:" << endl; c.display(); } else if (flag == 2) { c = a*b; cout << "The result of Matrix a multiply Matrix b is:" << endl; c.display(); } a.Matrixdel(); b.Matrixdel(); c.Matrixdel(); system("pause"); return 0; } void Matrix::Matrixnew() { mat = new int *[rows]; for (int i = 0; i < rows; i++) mat[i] = new int[columns]; for (int i = 0; i < rows; i++) for (int j = 0; j < columns; j++) mat[i][j] = 0; } void Matrix::Matrixdel() { for (int i = 0; i < rows; i++) delete mat[i]; delete mat; } Matrix operator+(Matrix &a, Matrix &b) { Matrix c; c.rows = a.rows; c.columns = b.columns; c.Matrixnew(); for (int i = 0; i < a.rows; i++) for (int j = 0; j < a.columns; j++) c.mat[i][j] = a.mat[i][j] + b.mat[i][j]; return c; } Matrix operator*(Matrix &a, Matrix &b) { Matrix c; c.rows = a.rows; c.columns = b.columns; c.Matrixnew(); for (int i = 0; i < a.rows; i++) for (int j = 0; j < b.columns; j++) for (int k = 0; k < a.columns; k++) c.mat[i][j] += a.mat[i][k] * b.mat[k][j]; return c; } void Matrix::input() { cout << "Please enter the elements of the matrix:" << endl; for (int i = 0; i < rows; i++) for (int j = 0; j < columns; j++) cin >> mat[i][j]; } void Matrix::display() { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) cout << mat[i][j] << '\t'; cout << endl; } cout << endl; } int main(void) { Matrix a, b, c; int i, j, k, flag = 0; cout << "Please enter the rows and the columns of the first matrix:"; cin >> i >> j; cout << "Please enter the columns of the second matrix:"; cin >> k; cout << "Which result do you want to get?" << endl; cout << "1.a+b=" << endl << "2.a*b" << endl; cin >> flag; a.rows = i; a.columns = j; b.rows = j; b.columns = k; c.rows = i; c.columns = k; a.Matrixnew(); b.Matrixnew(); c.Matrixnew(); a.input(); b.input(); if (flag == 1) { c = a + b; cout << "The result of Matrix a add Matrix b is:" << endl; c.display(); } else if (flag == 2) { c = a*b; cout << "The result of Matrix a multiply Matrix b is:" << endl; c.display(); } a.Matrixdel(); b.Matrixdel(); c.Matrixdel(); system("pause"); return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-971189.html

    最新回复(0)