Introduction concept:
Macro. Variables revvisited (local variables and scope, function parameters and actual parameters, passed by value and passed by address), programming style, void * type, variable naming. Program organization (function/file/project); default parameters
Selection introduction: class (OOP), class encapsulation, new/delete operator, construction, destructionexercises:
Use the class to repeat the task of the first lesson, Matrix. file: main(). Set 2: matrix.h, matrix.cpp
Claim:
Matrix addition, matrix multiplication. It is recommended to free space in the destruction
(Reference: C++ Advanced Programming Version 2 or 3, note that as long as you refer to Chapter 1. Don't read the others!)
#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;
}