return *this; }
Point Point::operator=(Point P) {
this->x-=p.x;this->y-=p.y; return *this; }
void main() {
Point pl(2,3),p2(3,4),p3(p2) cout<<\:p3.disp(); p3.offset(10,10);
cout<<\:\
cout<<\:\ < cout<<\:\ p3-=p2; cout<<\:\ p3=pl+p3; //先将pl+p3的结果放在p1中,然后赚给p3,所以pl=p3 cout<<\:\.disp(); p3=pl-p2; cout<<\} 24. 编写一个对具有n个元素的数组x[]求最大值的程序,要求将求最大值的函数设 计成函数模板。 【知识点】:3.6 【参考分】:20分 【难易度】:C 【答案】: #include int 1; T maxv=x[0]; for(i=1;i void main() { int a[]=(4,5,2,8,9,3}; double b[]={3.5,6.7,2,5.2,9.2}; cout<<\数组最大值:\,6)< 25. 编写一个函数模板,它返回两个值中较小者,同时要求能正确处理字符串。 【知识点】:3.6 【参考分】:20分 【难易度】:C 【答案】:设计一个函数模板template #include return(a char *min(char *a,char *b) { return(strcmp(a,b)<0?a:b); } void main() { double a=3.56,b=8.23; char s1[]=\,s2[]=\ cout<<\输出结果:\ cout<< \中较小者:\,b) < 26. 设计一个数组类模板Array #include T *elems; int size; public: Array(int S); //构造函数 ~Array(); T& operator[](int); //重载下标运算符 void operator=(T); //重载等号运算符 }; template size=s; elems=new T[SiZe]; for(int i=0;i template delete elems; } template return elems[index]; } template for(int i=0;i void main() { int 1,n=10; Array arr1[i]='a'+i; //调用重载运算符 arr2[i]='a'+i; } cout<<\ ASCII码 字符\; for(i=O;i cout< 一个Sample类模板的私有数据成员为T n,在该类模板中设计一个“operator==”重载运算符函数,用于比较各对象的n数据是否相等。 【知识点】:3.6 【参考分】:20分 【难易度】:C 【答案】: #include T n; public: Sample(T i){n=i;} int operator==(Sample &); }; template if(n==s.n) return 1; else return 0; } void main() { Sample cout<<\与s2的数据成员\相等\:\不相等\ Sample cout<<\与s4的数据成员\相等\不相等\} 28. 设计一个模板类Sample,用于对一个有序数组采用二分法查找元素下标。 【知识点】:3.6 【参考分】:25分 【难易度】:B 【答案】: #include Sample(){} Sample(T a[],int i); int seek(T c); void disp() { for (int i=0; i n=i; for(int j=O; j template int low=0,high=n-l,mid; while(low<=high) { mid=(low+high)/2; if(A[mid]==c) return mid; else if (A[mid] return -1; } void main() { char a[]=”acegkmpwxz”; Sample cout<<\元素序列:\.disp(); cout<<\元素'g'的下标:\.seek('g')< 29. 编写一个程序,设计一个点类Point,并由此派生出一个圆类。 【知识点】:5.1 5.2 【参考分】:20分 【难易度】:B 【答案】: #include int x,y; public: