C++中基于对象的编程风格的学习,非常有难度,概念很抽象,操作起来也比较费脑子,这里主要把一些知识点和习题给过一遍!
一、前言
C++中基于对象的编程风格的学习(Essential C++ 第四章)。
二、例题
-P235 练习 4.5
请实现一个4x4的Matrix class,至少提供以下接口:矩阵加法、矩阵乘法、打印函数 print()、复合运算符+=,以及一组支持下标操作(subscriping)的 function call 运算符,像下面这样:
float& operator()( int row,int column );
float operator()(int row,int column )const;
请提供一个 default constructor,可选择性地接受16个数据值。再提供一个 constructor,可接受个拥有16个元素的数组。你不需要为此class提供copy constructor copy assignmentoperator 、destructor
第6章重新实现 Matrix class 时才会需要这几个函数,用以支持任意行列的矩阵。
- 先分析一下,一共有这几件事情要做:
-
- 4x4的Matrix class;
-
- Matrix class支持:矩阵加法、矩阵乘法、打印函数 print()、复合运算符+=、下标操作;
-
- 提供一个 default constructor,可选择性地接受16个数据值。
#include <iostream>using namespace std;typedef float elemType;class Matrix
{// friend 声明一般放在class一开始之处friend Matrix operator+(const Matrix&, const Matrix&);friend Matrix operator*(const Matrix&, const Matrix&);public:Matrix(const elemType*);Matrix(elemType = 0.0, elemType = 0.0, elemType = 0.0, elemType = 0.0,elemType = 0.0, elemType = 0.0, elemType = 0.0, elemType = 0.0,elemType = 0.0, elemType = 0.0, elemType = 0.0, elemType = 0.0,elemType = 0.0, elemType = 0.0, elemType = 0.0, elemType = 0.0);// 获取矩阵的行列数int rows() const { return 4; }int cols() const { return 4; }// 打印函数ostream& print(ostream&) const;// += 函数void operator+=(const Matrix&);// 下标获取elemType operator() (int row,int col ) const{return _matrix[row][col];}elemType& operator()(int row, int col){return _matrix[row][col];}private:elemType _matrix[4][4];};// 重载输出
inline ostream& operator<<(ostream& os, const Matrix& m)
{return m.print(os);
}Matrix operator+(const Matrix &m1, const Matrix &m2)
{Matrix result(m1);result += m2;return result;
}
Matrix operator*(const Matrix &m1, const Matrix &m2)
{Matrix result;for (int ix = 0; ix < m1.rows(); ++ix){for (int jx = 0; jx < m1.cols(); ++jx){result(ix, jx) = 0;for (int kx = 0; kx < m1.cols(); ++kx){result(ix, jx) += m1(ix, kx) * m2(kx, jx);}}}return result;
}// 打印函数
ostream& Matrix::print(ostream &os) const
{int cnt = 0;for (int ix = -0; ix < 4; ++ix){for(int jx=0;jx<4;++jx,++cnt){if (cnt && !(cnt % 4))os << endl;os << _matrix[ix][jx] << ' ';}}os << endl;return os;
}
// += 函数
void Matrix::operator+=(const Matrix &m)
{for (int ix = 0; ix < 4; ++ix){for (int jx = 0; jx < 4; ++jx){_matrix[ix][jx] += m._matrix[ix][jx];}}
}Matrix::Matrix(const elemType* arr)
{int arr_idx = 0;for (int ix = 0; ix < 4; ++ix){for (int jx = 0; jx < 4; ++jx){_matrix[ix][jx] = arr[arr_idx++];}}
}Matrix::Matrix(elemType a11, elemType a12, elemType a13, elemType a14, elemType a21, elemType a22, elemType a23, elemType a24, elemType a31, elemType a32, elemType a33, elemType a34, elemType a41, elemType a42, elemType a43, elemType a44 )
{_matrix[0][0] = a11; _matrix[0][1] = a12; _matrix[0][2] = a13; _matrix[0][3] = a14;_matrix[1][0] = a21; _matrix[1][1] = a22; _matrix[1][2] = a23; _matrix[1][3] = a24;_matrix[2][0] = a31; _matrix[2][1] = a32; _matrix[2][2] = a33; _matrix[2][3] = a34;_matrix[3][0] = a41; _matrix[3][1] = a42; _matrix[3][2] = a43; _matrix[3][3] = a44;
}int main()
{Matrix m;cout << m << endl;elemType ar[16] = {1.,2.,3.,4.,2.,3.,4.,1.,3.,4.,1.,2.,4.,1.,2.,3.};// 赋值1Matrix identity(ar);cout << identity << endl;// 赋值2Matrix m2(identity);m = identity;cout << m2 << endl;cout << m << endl;// 赋值3elemType ar2[16] = {0.1,0.2,0.3,0.4,0.2,0.3,0.4,0.1,0.3,0.4,0.1,0.2,0.4,0.1,0.2,0.3,};Matrix m3(ar2); cout << m3 << endl;// 乘法Matrix m4 = m3 * identity;cout<< m4 << endl;// 加法Matrix m5 = m3 + m4;cout << m5 << endl;m3 += m4;cout << m3 << endl;}
- 这道题非常的赞哦,函数重载,友元,类函数,好美妙啊!
代码是在 visual studio 中编写的,该软件还是比较好用的,我安装的是2022专业版;
共勉!