编写类AA的成员函数int Compare(AAb), 该函数用于比较*this与b的大小,若两者含有元素的个数n相同,并且数组中前n个元素值对应相同,则认为两者相等返回1,否则返回0。注意:用数组方式及for循环来实现该函数。输出结果如下:
a=b
a<>C
注意:部分源程序已存在文件testl7_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数Compare的花括号中填写若干语句。
文件testl7_2.cpp的内容如下:
#include<iostream.h>
#include<stdlib.h>
class AA
int *a;
int n;
int MS;
public:
void InitAA(int aa[], int nn, int ms)
if(nn>ms)
cout<<"Error!"<<end1;
exit(1);
MS=ms;
n=nn;
a=new int[MS];
for(int i=0; i<n; i++) a[i]=aa[i];
int Compare(AA b);
;
int AA::Compare(AA b)
void main()
AA a,b,c;
int x[]=1,2,3,4,5;
int y[]=1,2,3,6,7;
int z[]=1,2,5,7,9;
- a. InitAA(x,3,5);
- b. InitAA(y,3,5);
- c. InitAA(z,3,5);
if (a.Compare(b))
cout<<"a=b"<<end1;
else
cout<<"a<>b"<<end1;
if (a.Compare(c))
cout<<"a=c"<<end1;
else
cout<<"a<>c"<<end1;