阅读下列C++程序和程序说明,将应填入(n)处的字句写在对应栏内。
【说明】
C++语言本身不提供对数组下标越界的判断。为了解决这一问题,在程序6中定义了相应的类模板,使得对厂任意类型的二维数组,可以在访问数组元素的同时,对行下标和列下标进行越界判断,并给出相应的提示信息。
include<iostream.h>
template <class T> class Array;
template <class T> class ArrayBody {
friend (1)
T* tpBody;
int iRows, iColumns, iCurrentRow;
ArrayBody (int iRsz, int iCsz) {
tpBody =(2)
iRows = iRsz; iColumns =iCsz; iCurrentRow =-1;
}
public:
T& operator[] (int j) {
bool row_error, column_error;
row_error=column_error=false;
try{
if (iCurrentRow < 0 || iCurrentRow >=iRows)
row_error=true;
if (j < 0 || j >=iColumns)
column_error=true;
if ( row_error==true || column_error == true)
(3)
}
catch (char) {
if (row_error==true)
cerr << "行下标越界[" << iCurrentRow << "] ";
if (column_error== true )
cerr << "列下标越界[" <<j << "]";
cout << "\n";
}
return tpBody[iCurrentRow * iColumns +j];
};
~ArrayBody ( ) { delete[] tpBody; }
};
template <class T> class Array {
ArrayBody<T> tBody;
public:
ArrayBody<T> & operator[] (int i) {
(4)
return tBody;
}
Array (int iRsz, int iCsz) :(5) {}
};
void main()
{ Array<int>a1(10,20);
Array<double>a2(3,5);
int b1;
double b2;
b1=a1[-5][10]; //有越界提示:行下标越界[-5]
b1=a1[10][15]; //有越界提示:行下标越界[10]
b1=a1[1][4]; //没有越界提示
b2=a2[2][6]; //有越界提示:列下标越界[6]
b2=s2[10][20]; //有越界提示:行下标越界[10]列下标越界[20]
b2=a2[1][4]; //没有越界提示
}
第1题:
阅读以下说明,以及用C++在开发过程中所编写的程序代码,将应填入(n)处的字句写在对应栏内。
【说明】
在下面函数横线处填上适当的字句,使其输出结果为:
构造函数.
构造函数.
1,2
5,6
析构函数
析构函数.
【C++代码】
include "iostream.h"
class AA
{ public;
AA(int i,int j)
{A=i; B=j;
cout<<"构造函数.\n";
}
~AA(){(1);}
void print();
private:
int A, B;
};
void AA∷print()
{cout<<A<<","<<B<<endl;}
void main()
{
AA *a1, *a2;
(2)=new AA(1, 2);
a2=new AA(5, 6);
(3);
a2->print();
(4) a1;
(5) a2;
}
第2题:
试题三(共 15 分)
阅读以下说明和 C 程序,将应填入 (n) 处的字句写在答题纸的对应栏内。
第3题:
第4题:
阅读下列程序说明和C++程序,把应填入其中(n)处的字句,写在对应栏内。
【说明】
阅读下面几段C++程序回答相应问题。
比较下面两段程序的优缺点。
①for (i=0; i<N; i++ )
{
if (condition)
//DoSomething
…
else
//DoOtherthing
…
}
②if (condition) {
for (i =0; i<N; i++ )
//DoSomething
}else {
for (i=0; i <N; i++ )
//DoOtherthing
…
}
第5题:
第6题: