運算符重載編程題3(C++程序設計第4周)

 

描述

寫一個二維數組類 Array2,使得下面程序的輸出結果是:

0,1,2,3,

4,5,6,7,

8,9,10,11,

next

0,1,2,3,

4,5,6,7,

8,9,10,11,

程序:

#include <iostream>
#include <cstring>
using namespace std;
// 在此處補充你的代碼
int main() {
    Array2 a(3,4);                      //調用帶參數的構造函數
    int i,j;
    for( i = 0;i < 3; ++i )
        for( j = 0; j < 4; j ++ )
            a[i][j] = i * 4 + j;        //調用運算符[ ]重載函數
    for( i = 0;i < 3; ++i ) {
        for( j = 0; j < 4; j ++ ) { 
            cout << a(i,j) << ",";      //調用運算符()重載函數
        }
        cout << endl;
    }
    cout << "next" << endl;
    Array2 b;                           //調用無參構造函數
    b = a;                              //調用運算符=重載函數                      
    for( i = 0;i < 3; ++i ) {
        for( j = 0; j < 4; j ++ ) {
            cout << b[i][j] << ",";      //調用運算符[ ]重載函數
        }
        cout << endl;
    }
    return 0;
}

分析

需要編寫一個類,這個類包括:

  • 無參構造函數
  • 有參數構造函數
  • 析構函數
  • 複製構造函數(可以不寫)
  • 重載=運算符
  • 重載[ ]運算符
  • 重載( )運算符

代碼:

#include <iostream>
#include <cstring>

using namespace std;

// 代碼開始
class Array2{

private:
	int row; 
	int column;
	int *ptr;  //指向二維數組的指針

public:
	//無參構造函數
	Array2( ){
		ptr = NULL;
	}
	//構造函數
	Array2(int paraRow, int paraColumn) :row(paraRow), column(paraColumn){
		ptr = new int[row*column]; //創建一個數組,大小爲row*column,並複製給指針ptr
	}
	//複製構造函數
	Array2(Array2& a) :row(a.row), column(a.column){
		ptr = new int[row*column];
		memcpy(ptr, a.ptr, sizeof(int)*row*column);
	}

	~Array2(){
		if (ptr)
			delete[]ptr;
	}

	//運算符=重載
	Array2& operator= (const Array2 &a){
		if (ptr == a.ptr){
			return *this;
		}
		if (ptr)
			delete[]ptr;
		if (a.ptr){
			row = a.row;
			column = a.column;
			ptr = new int[row*column];
			memcpy(ptr, a.ptr, sizeof(int)*row*column);
		}
		else
		{
			row = 0;
			column = 0;
			ptr = NULL;
		}
		return *this;
	}

	//運算符[]重載
	int* operator[](int i){
		return ptr + i*column;//重載的實際上是第二維的[], 第一維的[]直接調用int型一維數組的定義
	}
	//運算符()重載
	int& operator()(int i, int j){
		return ptr[i*column + j];
	}
	//代碼結束
};

int main() {
	Array2 a(3, 4);
	int i, j;
	for (i = 0; i < 3; ++i)
		for (j = 0; j < 4; j++)
			a[i][j] = i * 4 + j;

	for (i = 0; i < 3; ++i) {
		for (j = 0; j < 4; j++) {
			cout << a(i, j) << ",";
		}
		cout << endl;
	}

	cout << "next" << endl;

	Array2 b;
	b = a;
	for (i = 0; i < 3; ++i) {
		for (j = 0; j < 4; j++) {
			cout << b[i][j] << ",";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

代碼分析

  • 重載[]運算符
int* operator[](int i){
	return ptr + i*column;
}

分析:
main()函數中的a[i][j]等價於(a.[ ](i)).[j];
先調用該函數將指針指向二維數組的第i行的開頭,返回的值是int* 指針,假定爲p;
p[j]得到第i行的第j個值。

 

  • 重載()運算符
int& operator()(int i, int j){
    return ptr[i*column + j];
}

分析:

main()函數中的a(i,j)等價於a.operator()(i,j);

return的結果是ptr[i*column+j],兩個for循環實現從ptr[0]到ptr[(i-1)*column+j-1],也就遍歷了二維數組中所有的數;

返回值類型是int & ;

 

  • 重載=運算符
Array2& operator= (const Array2 &a){
	if (ptr == a.ptr){
		return *this;
	}
	if (ptr)
		delete[]ptr;
	if (a.ptr){
		row = a.row;
		column = a.column;
		ptr = new int[row*column];
		memcpy(ptr, a.ptr, sizeof(int)*row*column);
	}
	else
	{
		row = 0;
		column = 0;
		ptr = NULL;
	}
	return *this;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章