小編給大家分享一下C++中重載和運算符重載加號實現矩陣相加的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
C++ 重載+運算符重載加號 實現矩陣相加
實例代碼:
#include<iostream>
#include<iomanip>
using namespace std;
class Complex
{
private:
int i,j,n,a[2][3];
public:
Complex();
Complex operator+(Complex &c);
void display();
void input();
} t1;
Complex::Complex()
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
a[i][j]=0;
}
void Complex::input()
{
for(i=0;i<2;i++)
for(j=0;j<3;j++)
cin>>a[i][j];
}
void Complex::display()
{
n=1;
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
n++;
if(n%2==0)
cout<<endl;
cout<<setw(5)<<a[i][j];
}
cout<<endl;
}
Complex Complex::operator+(Complex &c)
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
t1.a[i][j]=a[i][j]+c.a[i][j];
return t1;
}
int main()
{
Complex t2,t3,t4;
cout<<"請輸入2*3個整數"<<endl;
t2.input();
cout<<endl;
cout<<"請輸入2*3個整數"<<endl;
t3.input();
cout<<endl;
cout<<"t2";
t2.display();
cout<<"t3";
t3.display();
t4=t2+t3;
cout<<"t4=t2+t3=";
t4.display();
return 0;
}以上是“C++中重載和運算符重載加號實現矩陣相加的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。