溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++中重載和運算符重載加號實現(xiàn)矩陣相加的示例分析

發(fā)布時間:2021-07-22 09:41:00 來源:億速云 閱讀:227 作者:小新 欄目:編程語言

小編給大家分享一下C++中重載和運算符重載加號實現(xiàn)矩陣相加的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

 C++ 重載+運算符重載加號 實現(xiàn)矩陣相加

實例代碼:

#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個整數(shù)"<<endl; 
  t2.input();
  cout<<endl;
  cout<<"請輸入2*3個整數(shù)"<<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++中重載和運算符重載加號實現(xiàn)矩陣相加的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI