C++之运算符重载案例

  • 不是所有的操作符都能重载,下面操作符不能重载
cpp 复制代码
:: . ?: sizeof typeid
  • 实现一个3*3的矩阵类,支持如下操作符:
    • 运算类双目操作符:+ - *
    • 赋值类双目操作符:+= -= *=
    • 单目操作符:-(相反数)
    • 输出操作符:<<
cpp 复制代码
#include <iostream>
using namespace std;
class Mat33{
private:
    int m_a[3][3];
public:
    Mat33(void){
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                m_a[i][j] = 0;
            }
        }
    }
    Mat33(int a[][3]){
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                m_a[i][j] = a[i][j];
            }
        }
    }
    // + a+b
    const Mat33 operator+(const Mat33& m) const{
        int a[3][3] = {0};
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                a[i][j] = m_a[i][j] + m.m_a[i][j];
            }
        }
        Mat33 result(a);
        return result;
    }
    // - a-b
    const Mat33 operator-(const Mat33& m) const{
        int a[3][3] = {0};
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                a[i][j] = m_a[i][j] - m.m_a[i][j];
            }
        }
        Mat33 result(a);
        return result;
    }
    // * a*b
    const Mat33 operator*(const Mat33& m) const{
        int a[3][3] = {0};
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                for(int k=0; k<3; k++){
                    a[i][j] += m_a[i][k] * m.m_a[k][j];
                }
            }
        }
        Mat33 result(a);
        return result;
    }
    // += a += b;
    Mat33& operator+=(const Mat33& m){
    *this = *this + m; // operator+
    return *this;
    }
    // -= a -= b;
    Mat33& operator-=(const Mat33& m){
        *this = *this - m; // operator-(m)
        return *this;
    }
    // *= a *= b;
    Mat33& operator*=(const Mat33& m){
        *this = *this * m; // operator*
        return *this;
    }
    //-(取负) -a;
    const Mat33 operator-(void) const{
        Mat33 m;
        return m - *this; // operator-(m)
    }
    /*
    * os << a;
    * */
    friend ostream& operator<<(ostream& os, const Mat33& m){
        for(int i=0; i<3; i++){
            for(int j=0; j<3; j++){
                os << m.m_a[i][j] << " ";
            }
            cout << endl;
        }
        return os;
    }
};
int main(void){
    int a1[3][3] = {1,2,3,4,5,6,7,8,9};
    int a2[3][3] = {9,8,7,6,5,4,3,2,1};
    Mat33 m1(a1);
    Mat33 m2(a2);
    cout << m1 << endl;
    cout << m2 << endl;
    cout << "m1+m2: "<< endl;
    cout << m1 + m2 << endl;
    cout << "m1-m2: "<< endl;
    cout << m1 - m2 << endl;
    cout << "m1*m2: "<< endl;
    cout << m1 * m2 << endl;
    cout << "m1 += m2: "<< endl;
    cout << (m1 += m2) << endl;
    cout << "m1 -= m2: "<< endl;
    cout << (m1 -= m2) << endl;
    cout << "m1 *= m2: "<< endl;
    cout << (m1 *= m2) << endl;
    cout << "-m2" << endl;
    cout << -m2 << endl;
    return 0;
}
相关推荐
John.Lewis5 分钟前
C++进阶(12)附加学习:STL之空间配置器(了解)
开发语言·c++·笔记
汉克老师14 分钟前
GESP2023年12月认证C++三级( 第三部分编程题(2、单位转换))
c++·string·单位转换·gesp三级·gesp3级
cpp_25011 小时前
P2347 [NOIP 1996 提高组] 砝码称重
数据结构·c++·算法·题解·洛谷·noip·背包dp
Hugh-Yu-1301231 小时前
二元一次方程组求解器c++代码
开发语言·c++·算法
楼田莉子2 小时前
同步/异步日志系统:日志落地模块\日志器模块\异步日志模块
linux·服务器·c++·学习·设计模式
文祐2 小时前
C++类之虚函数表及其内存布局
开发语言·c++
小狄同学呀2 小时前
同样的global,不同的audioLibPath——记一次诡异的内存错位
c++·windows
编程大师哥2 小时前
C++类和对象
开发语言·c++·算法
Rabitebla2 小时前
C++ 和 C 语言实现 Stack 对比
c语言·数据结构·c++·算法·排序算法
旖-旎2 小时前
递归(汉诺塔问题)(1)
c++·学习·算法·leetcode·深度优先·递归