有两个矩阵a和b,均为2行3列。用面向对象的方法求两个矩阵之和。要求根据main( )函数中的操作去设计M类,在该类中根据运算符操作的要求去重载相应的运算符。两矩阵相加c =a+b :输入第一个矩

这篇博客展示了如何使用C++的面向对象编程来设计一个M类,并重载运算符+和<<,以实现两个2行3列矩阵的相加和打印。通过友元函数实现矩阵元素的输入、输出和相加操作,最后在main函数中进行实例化并展示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
using namespace std;

class M
{
private:
    int Q[2][3];
public:
    friend M operator + (M& x, M& y);
    friend ostream& operator<<(ostream& output, M& a);
    friend istream& operator>>(istream& input, M& a);
};
M operator + (M& x, M& y)
{
    M c;
    int i, j;
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            c.Q[i][j] = x.Q[i][j] + y.Q[i][j];
        }
    }
    return c;
}
ostream& operator<<(ostream& output, M& a)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值