从右向左哦~ 其实,通过设置断点走一遍就知道了
#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;
}
本文展示了一个简单的C++程序,定义了一个名为Matrix的类,并实现了两个Matrix对象的加法操作。通过实例演示了如何创建Matrix对象并进行加法运算,最后输出结果。

被折叠的 条评论
为什么被折叠?



