C++输出操作符求值顺序

本文展示了一个简单的C++程序,定义了一个名为Matrix的类,并实现了两个Matrix对象的加法操作。通过实例演示了如何创建Matrix对象并进行加法运算,最后输出结果。

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

从右向左哦~ 其实,通过设置断点走一遍就知道了吐舌头

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


class Matrix
{
public:
Matrix(int m,int n):a(m),b(n){}
Matrix(){}
friend Matrix operator + (const Matrix& pre, const Matrix& mat);
Matrix(const Matrix& A){ a=A.a;b=A.b;cout<<"拷贝构造函数被调用"<<endl;}
int getA(){return a;}
int getB(){return b;}
private:
int a,b;
};


Matrix operator + (const Matrix& pre, const Matrix& mat)
{
Matrix M;
M.a=pre.a+mat.a;
M.b=pre.b+mat.b;
return M;
}


int  main(int argc,  char* argv[])
{
cout<<"start:"<<endl;
Matrix A(1,3);
Matrix B(2,4);
Matrix C=A+B;
cout<<C.getA()<<(C.getB()+1)<<" "<<C.getB()<<endl;


system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值