`

C++ 之操作符重载

阅读更多

//操作符重载:
/******************************************************************************************
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;
class arry
{
 public:
  arry(int x[2][2])
  {
   for(int i=0;i<2;i++)
   {
    for(int j=0;j<2;j++)
    {
     arr[i][j]=x[i][j];
    }
   }
  }
  arry operator+(arry b);
  void display();
 private:
  int arr[2][2];

};
arry arry::operator+(arry b)
{
 int c[2][2];
 for(int i=0;i<2;i++)
 {
  for(int j=0;j<2;j++)
  {
   c[i][j]=arr[i][j]+b.arr[i][j];
  }
 }
 return arry(c);
}

void arry::display()//自己写display的实现吧:)
{
}

int main(int argc,_TCHAR* argv[])
{
 int a1[2][2]={1,2,3,4},b1[2][2]={1,2,3,4},c1[2][2]; //注意这两行的修改
 arry a(a1),b=(b1),c(c1); //注意这两行的修改
 cout<<"a=";a.display();
 cout<<"b=";b.display();
 c=a+b;
 cout<<"c=a+b=";
 c.display();
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics