运算符重载编程题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;
}

猜你喜欢

转载自blog.csdn.net/weixin_41074793/article/details/88119362